二叉树part3

二叉树Part3

题目如下:

110. 平衡二叉树 - 力扣(LeetCode)

第一想法,比较根节点左右子树高度即可,这与题目平衡二叉树的概念相悖

遂改正,并成功ac

class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root == null){  //返回条件
            return true;
        }
        if(!isOneBalanced(root)){  //节点是否平衡,一旦不平衡,直接返回false;
            return false;
        }
        boolean left = isBalanced(root.left);
        boolean right = isBalanced(root.right);
        return left && right;
    }

    public boolean isOneBalanced(TreeNode root) {  //判断当前root节点是否平衡
        if(root == null){
            return true;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return !(left - right > 1 || right - left > 1);
    }

    public int maxDepth(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        return Math.max(leftDepth, rightDepth) + 1;
    }
}

但是自己理不清思路,ai归纳出这是前序的写法,并且其中有一些重复性计算

标准的答案很巧妙,在单层逻辑上实现计算高度的同时,判断是否平衡

class Solution {
    //-1表示不是平衡二叉树
    public int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHeight = getHeight(root.left); //左
        if(leftHeight == -1){
            return -1;
        }
        int rightHeight = getHeight(root.right); //右
        if(rightHeight == -1){
            return -1;
        }
        return Math.abs(leftHeight - rightHeight) > 1 ? -1 : Math.max(leftHeight, rightHeight) + 1; //中
    }

    public boolean isBalanced(TreeNode root) {
        return getHeight(root) != -1;
    }
}
257. 二叉树的所有路径 - 力扣(LeetCode)

第一想法,有几个叶子节点,就有几条路径

按照递归的做题思路来复盘:

  • 递归函数参数以及返回值
void traversal(TreeNode cur, List<Integer> path, List<String> res)

path用于记录路径和回溯

res用于记录结果

  • 确定递归终止条件

往常递归都是空节点返回,这个题目应该遇到叶子节点就向res中添加一条路径,跟我的第一想法不谋而合

  • 确定单层递归逻辑

这道题目使用前序?为什么

中:添加中.val进集合

左右:递归+回溯

注意本题递归和回溯放在同一个花括号里,因为递归和回溯是一一对应

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        List<Integer> path = new ArrayList<>();
        traversal(root, path, res);
        return res;
    }

    public void traversal(TreeNode cur, List<Integer> path, List<String> res){
        path.add(cur.val);
        if(cur.left == null && cur.right == null){ // 中mid 如果当前节点为叶子节点,那么向res中添加一条路径
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < path.size() - 1; i++){
                sb.append(path.get(i).toString());
                sb.append("->");
            }
            sb.append(path.get(path.size() - 1).toString());
            res.add(sb.toString());
        }
        if(cur.left != null){
            traversal(cur.left, path, res);  //左
            path.remove(path.size() - 1);  //回溯
        }
        if(cur.right != null){
            traversal(cur.right, path, res);  //右
            path.remove(path.size() - 1);  //回溯
        }
    }
}
404. 左叶子之和 - 力扣(LeetCode)

感觉ac后也不理解,抽象

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) return 0;
        int leftValue = sumOfLeftLeaves(root.left);    // 左
        int rightValue = sumOfLeftLeaves(root.right);  // 右
                                                       
        int midValue = 0;
        if (root.left != null && root.left.left == null && root.left.right == null) { 
            midValue = root.left.val;
        }
        int sum = midValue + leftValue + rightValue;  // 中
        return sum;
    }
}
222. 完全二叉树的节点个数 - 力扣(LeetCode)

普通二叉树用层序遍历求解即可

完全二叉树有特殊性质,只分两种情况,一种底层叶子未满,一种满

那么,如果向左遍历和向右遍历int相等,就是底层叶子满的情况

class Solution {
    public int countNodes(TreeNode root) {
        if(root == null){
            return 0;
        }
        TreeNode left = root.left;
        TreeNode right = root.right;
        int leftDepth = 0, rightDepth = 0;
        while(left != null){
            left = left.left;
            leftDepth++;
        }
        while(right != null){
            right = right.right;
            rightDepth++;
        }
        if(leftDepth == rightDepth){
            return (2 << leftDepth) - 1;
        }
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}
posted @ 2025-06-07 20:08  泡芙猪  阅读(19)  评论(0)    收藏  举报