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

文章目录

一. 力扣 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);
    }
}
相关推荐
小O的算法实验室18 小时前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
郭涤生19 小时前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿19 小时前
vector
c语言·开发语言·数据结构·c++·算法
Ricky111zzz19 小时前
leetcode学python记录1
python·算法·leetcode·职场和发展
汀、人工智能19 小时前
[特殊字符] 第58课:两个正序数组的中位数
数据结构·算法·数据库架构··数据流·两个正序数组的中位数
liu****19 小时前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯
汀、人工智能19 小时前
[特殊字符] 第79课:分割等和子集
数据结构·算法·数据库架构·位运算·哈希表·分割等和子集
汀、人工智能19 小时前
[特殊字符] 第74课:完全平方数
数据结构·算法·数据库架构·图论·bfs·完全平方数
CoderCodingNo20 小时前
【GESP】C++四、五级练习题 luogu-P1177 【模板】排序
数据结构·c++·算法
Proxy_ZZ020 小时前
从零实现LDPC比特翻转译码器:C语言实战与底层逻辑解析
c语言·算法