算法奇妙屋(二十六)-二叉树的深度搜索问题

文章目录

一. 力扣 2331. 计算布尔二叉树的值

1. 题目解析

2. 算法原理

依旧是递归三部曲

3. 代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean evaluateTree(TreeNode root) {
        if (root.left == null && root.right == null) {
            return root.val == 1;
        }
        boolean left = evaluateTree(root.left);
        boolean right = evaluateTree(root.right);
        return root.val == 2 ? left || right : left && right;
    }
}

二. 力扣 129. 求根节点到叶节点数字之和

1. 题目解析

2. 算法原理

3. 代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int sumNumbers(TreeNode root) {
        return dfs(root, 0);
    }

    public int dfs(TreeNode root, int pre) {
        int cur = pre * 10 + root.val;
        if (root.left == null && root.right == null) {
            return cur;
        }
        int left = 0;
        if (root.left != null) {
            left = dfs(root.left, cur);

        }
        int right = 0;
        if (root.right != null) {
            right = dfs(root.right, cur);
        }
        return left + right;
    }
}

三. 力扣 814. 二叉树剪枝

1. 题目解析

2. 算法原理

3. 代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode pruneTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        root.left = pruneTree(root.left);
        root.right = pruneTree(root.right);
        if (root.left == null && root.right == null && root.val == 0) {
            root = null;
        }
        return root;
    }
}

四. 力扣 98. 验证二叉搜索树

1. 题目解析

最重要的是二叉搜索树的定义, 以及空树也是二叉搜索树

2. 算法原理

3. 代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    long prev = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        boolean left = isValidBST(root.left);
        boolean cur = false;
        if (prev < root.val) {
            prev = root.val;
            cur = true;
        }
        boolean right = isValidBST(root.right);
        return left && right && cur;
    }
}

4. 剪枝后的代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    long prev = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        boolean left = isValidBST(root.left);
        // 剪枝
        if (!left) {
            return false;
        }
        // 剪枝
        if (prev < root.val) {
            prev = root.val;
        }else {
            return false;
        }
        boolean right = isValidBST(root.right);
        return left && right;
    }
}

五. 力扣 230. 二叉搜索树中第 K 小的元素

1. 题目解析

第k小的数, 就是中序遍历后第k个数

2. 算法原理

3. 代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int count;
    int ret = 0;

    public int kthSmallest(TreeNode root, int k) {
        count = k;
        inorder(root);
        return ret;
    }
    public void inorder(TreeNode root) {
        if (root == null || count <= 0) {
            return;
        }
        if (count == 0) {
            return;
        }
        inorder(root.left);
        if (--count == 0) {
            ret = root.val;
            return;
        }
        inorder(root.right);
    }
}

六. 力扣 257. 二叉树的所有路径

1. 题目解析

2. 算法原理

3. 代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    List<String> ret = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        dfs(root, new StringBuffer());
        return ret;
    }
    void dfs(TreeNode root, StringBuffer o_path) {
        if (root == null) {
            return;
        }
        StringBuffer path = new StringBuffer(o_path);
        path.append(root.val);
        if (root.left == null && root.right == null) {
            ret.add(path.toString());
            return;
        }
        path.append("->");
        dfs(root.left, path);
        dfs(root.right, path);
    }
}
相关推荐
一叶落43815 小时前
题目:15. 三数之和
c语言·数据结构·算法·leetcode
老鱼说AI16 小时前
CUDA架构与高性能程序设计:异构数据并行计算
开发语言·c++·人工智能·算法·架构·cuda
罗湖老棍子16 小时前
【例 1】数列操作(信息学奥赛一本通- P1535)
数据结构·算法·树状数组·单点修改 区间查询
big_rabbit050217 小时前
[算法][力扣222]完全二叉树的节点个数
数据结构·算法·leetcode
张李浩17 小时前
Leetcode 15三题之和
算法·leetcode·职场和发展
2301_7938046918 小时前
C++中的适配器模式变体
开发语言·c++·算法
x_xbx18 小时前
LeetCode:206. 反转链表
算法·leetcode·链表
abant218 小时前
leetcode 138 复制随机链表
算法·leetcode·链表
ab15151718 小时前
3.17二刷基础112 118 完成进阶52
数据结构·算法
旖-旎19 小时前
二分查找(1)
c++·算法·二分查找·力扣·双指针