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

文章目录

一. 力扣 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);
    }
}
相关推荐
颜酱9 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
m0_736919109 小时前
C++代码风格检查工具
开发语言·c++·算法
yugi9878389 小时前
基于MATLAB强化学习的单智能体与多智能体路径规划算法
算法·matlab
DuHz10 小时前
超宽带脉冲无线电(Ultra Wideband Impulse Radio, UWB)简介
论文阅读·算法·汽车·信息与通信·信号处理
Polaris北极星少女10 小时前
TRSV优化2
算法
代码游侠11 小时前
C语言核心概念复习——网络协议与TCP/IP
linux·运维·服务器·网络·算法
2301_7634724611 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
abluckyboy12 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
园小异12 小时前
2026年技术面试完全指南:从算法到系统设计的实战突破
算法·面试·职场和发展
m0_7066532312 小时前
分布式系统安全通信
开发语言·c++·算法