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

文章目录

一. 力扣 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);
    }
}
相关推荐
2401_841495641 小时前
【LeetCode刷题】二叉树的中序遍历
数据结构·python·算法·leetcode··递归·遍历
2301_817497332 小时前
C++中的适配器模式实战
开发语言·c++·算法
砚边数影2 小时前
逻辑回归实战(一):用户流失预测数据集设计,KingbaseES存储标签数据
java·人工智能·算法·机器学习·逻辑回归·线性回归·金仓数据库
范纹杉想快点毕业2 小时前
自学嵌入式系统架构设计:有限状态机入门完全指南,C语言,嵌入式,单片机,微控制器,CPU,微机原理,计算机组成原理
c语言·开发语言·单片机·算法·microsoft
星火开发设计5 小时前
枚举类 enum class:强类型枚举的优势
linux·开发语言·c++·学习·算法·知识
嘴贱欠吻!10 小时前
Flutter鸿蒙开发指南(七):轮播图搜索框和导航栏
算法·flutter·图搜索算法
张祥64228890410 小时前
误差理论与测量平差基础笔记十
笔记·算法·机器学习
qq_1927798711 小时前
C++模块化编程指南
开发语言·c++·算法
cici1587413 小时前
大规模MIMO系统中Alamouti预编码的QPSK复用性能MATLAB仿真
算法·matlab·预编码算法