(二叉树04) 平衡二叉树 二叉树的所有路径 左叶子之和

一、平衡二叉树

力扣第110题:

重点:求深度用前序遍历,求高度用后序遍历!!!

(递归)后序遍历代码:

class Solution {
    public boolean isBalanced(TreeNode root) {
        return getHeight(root) != -1;
        
    }
    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int left = getHeight(root.left);
        if(left == -1) return -1;
        int right = getHeight(root.right);
        if(right == -1) return -1;

        if(Math.abs(left - right) > 1) {
            return -1;
        }
        return Math.max(left, right) + 1;
    }
}

二、二叉树的所有路径

力扣第257题:

(递归)前序遍历代码:

class Solution {
    List<String> list = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        preOrder(root, "");
        return list;
    }
    public void preOrder(TreeNode root, String sb) {
        StringBuilder str = new StringBuilder();
        str.append(sb);

        if(!sb.isEmpty()) {
            str.append("->");
        }
        str.append(root.val);

        if(root.left == null && root.right == null) {
            list.add(str.toString());
            return;
        }
        
        if(root.left != null) {
            preOrder(root.left, str.toString());
        }
        if(root.right != null) {
            preOrder(root.right, str.toString());
        }
    }
}

改进:

        先将结点值加入路径中,然后判断是不是叶子结点,若是,则将路径加入集合,若不是,则将"->"加入路径。(先加值,再加符号)

(递归)值传递改进代码:

class Solution {
    List<String> res = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        traversal(root, "");
        return res;
    }
    public void traversal(TreeNode root, String s) {
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            res.add(new StringBuilder(s).append(root.val).toString());
            return;
        }
        String temp = new StringBuilder(s).append(root.val).append("->").toString();
        traversal(root.left, temp);
        traversal(root.right, temp);
    }
}

上面的代码都是每递归一次创建一个新的StringBuilder对像,即路径传递的是值,而不是引用,无需路径回溯,以下代码是引用传递

(递归)引用传递代码:

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();// 存最终的结果
        if (root == null) {
            return res;
        }
        List<Integer> paths = new ArrayList<>();// 作为结果中的路径
        traversal(root, paths, res);
        return res;
    }

    private void traversal(TreeNode root, List<Integer> paths, List<String> res) {
        paths.add(root.val);// 前序遍历,中
        // 遇到叶子结点
        if (root.left == null && root.right == null) {
            // 输出
            StringBuilder sb = new StringBuilder();// StringBuilder用来拼接字符串,速度更快
            for (int i = 0; i < paths.size() - 1; i++) {
                sb.append(paths.get(i)).append("->");
            }
            sb.append(paths.get(paths.size() - 1));// 记录最后一个节点
            res.add(sb.toString());// 收集一个路径
            return;
        }
        // 递归和回溯是同时进行,所以要放在同一个花括号里
        if (root.left != null) { // 左
            traversal(root.left, paths, res);
            paths.remove(paths.size() - 1);// 回溯
        }
        if (root.right != null) { // 右
            traversal(root.right, paths, res);
            paths.remove(paths.size() - 1);// 回溯
        }
    }
}

(迭代)遍历代码:

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        if(root == null) {
            return null;
        }
        List<String> res = new ArrayList<>();
        Stack<Object> stack = new Stack<>();
        stack.push(root);
        stack.push(root.val + "");
        while(!stack.isEmpty()) {
            String s = (String)stack.pop();
            TreeNode node = (TreeNode)stack.pop();
            if(node.left == null && node.right == null) {
                res.add(s);
            }
            if(node.left != null) {
                stack.push(node.left);
                stack.push(new StringBuilder(s).append("->").append(node.left.val).toString());
            }
            if(node.right != null) {
                stack.push(node.right);
                stack.push(new StringBuilder(s).append("->").append(node.right.val).toString());
            }
        }
        return res;
    }
}

三、左叶子之和

力扣第404题:

(递归)全局变量代码如下:

class Solution {
    private int count = 0;
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left != null && root.left.left == null && root.left.right == null) {
            count += root.left.val;

        }
        sumOfLeftLeaves(root.left);
        sumOfLeftLeaves(root.right);
        return count;
    }
}

(递归)局部变量后序遍历:

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int value = 0;
        if(root.left != null && root.left.left == null && root.left.right == null) {
            value = root.left.val;
        }
        int leftValue = sumOfLeftLeaves(root.left);
        int rightValue = sumOfLeftLeaves(root.right);
        return leftValue + rightValue + value;
    }
}

(迭代)代码如下:

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null) {
            return 0;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        int count = 0;
        while(!stack.isEmpty()) {
            TreeNode node = stack.pop();
            if(node.left != null && node.left.left == null && node.left.right == null) {
                count += node.left.val;
            }
            if(node.left != null) {
                stack.push(node.left);
            }
            if(node.right != null) {
                stack.push(node.right);
            }
        }
        return count;
    }
}

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值