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

文章目录

一. 力扣 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);
    }
}
相关推荐
草履虫建模10 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
naruto_lnq12 小时前
分布式系统安全通信
开发语言·c++·算法
Jasmine_llq13 小时前
《P3157 [CQOI2011] 动态逆序对》
算法·cdq 分治·动态问题静态化+双向偏序统计·树状数组(高效统计元素大小关系·排序算法(预处理偏序和时间戳)·前缀和(合并单个贡献为总逆序对·动态问题静态化
爱吃rabbit的mq13 小时前
第09章:随机森林:集成学习的威力
算法·随机森林·集成学习
(❁´◡`❁)Jimmy(❁´◡`❁)14 小时前
Exgcd 学习笔记
笔记·学习·算法
YYuCChi14 小时前
代码随想录算法训练营第三十七天 | 52.携带研究材料(卡码网)、518.零钱兑换||、377.组合总和IV、57.爬楼梯(卡码网)
算法·动态规划
不能隔夜的咖喱15 小时前
牛客网刷题(2)
java·开发语言·算法
VT.馒头15 小时前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript
进击的小头15 小时前
实战案例:51单片机低功耗场景下的简易滤波实现
c语言·单片机·算法·51单片机
咖丨喱17 小时前
IP校验和算法解析与实现
网络·tcp/ip·算法