代码随想录算法训练营第十七天 | 110.平衡二叉树、257. 二叉树的所有路径、404.左叶子之和

110.平衡二叉树

题目链接:https://leetcode.cn/problems/balanced-binary-tree/
文档讲解:https://programmercarl.com/0110.%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91.html
视频讲解:https://www.bilibili.com/video/BV1Ug411S7my

思路

后序遍历的递归法

代码

class Solution {
    public boolean isBalanced(TreeNode root) {
        if (getHeight(root) == -1) return false;
        return true;
    }

    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;
        if (Math.abs(rightHeight - leftHeight) > 1) return -1; // 当左右子树高度差大于1,则不平衡,返回-1
        return 1 + Math.max(leftHeight, rightHeight); // 下面的子树都平衡,返回该节点的高度
    }
}

257. 二叉树的所有路径

题目链接:https://leetcode.cn/problems/binary-tree-paths/
文档讲解:https://programmercarl.com/0257.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%89%80%E6%9C%89%E8%B7%AF%E5%BE%84.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE
视频讲解:https://www.bilibili.com/video/BV1ZG411G7Dh

思路

前序遍历的递归法 + 回溯

代码

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<Integer> temp = new ArrayList<>();
        List<String> res = new ArrayList<>();
        allPath(root, temp, res);
        return res;
    }

    public void allPath(TreeNode root, List<Integer> temp, List<String> res) {
        temp.add(root.val);
        if (root.left == null && root.right == null) {
            StringBuilder path = new StringBuilder();
            for(int i = 0; i < temp.size() - 1; i++) {
                path.append(temp.get(i)).append("->");
            }
            path.append(temp.get(temp.size() - 1)); // 加入最后一个节点,后面没有"->"
            res.add(path.toString());
        }
        if (root.left != null) {
            allPath(root.left, temp, res);
            temp.remove(temp.size() - 1); // 回溯
        }
        if (root.right != null) {
            allPath(root.right, temp, res);
            temp.remove(temp.size() - 1); // 回溯
        }
    }
}

404.左叶子之和

题目链接:https://leetcode.cn/problems/sum-of-left-leaves/
文档讲解:https://programmercarl.com/0404.%E5%B7%A6%E5%8F%B6%E5%AD%90%E4%B9%8B%E5%92%8C.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE
视频讲解:https://www.bilibili.com/video/BV1GY4y1K7z8

思路

结果也是从下往上累加返回,使用后序遍历的递归。
由于需要判断左叶子,所以判断父节点的左孩子的左右孩子是否为空。

代码

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        return getLeftSum(root);
    }

    public int getLeftSum(TreeNode root) {
        if (root == null) return 0;
        if (root.left == null && root.right == null) return 0;
        int leftSum = getLeftSum(root.left);
        if (root.left != null && root.left.left == null && root.left.right == null) {
            leftSum = root.left.val;
        }
        int rightSum = getLeftSum(root.right);
        return leftSum + rightSum;
    }
}

之前准备面试落下好几天的任务,这两天的博客就简单写了,具体思路注释在代码里。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值