训练营练习-Day17&二叉树4

Leetcode 110 力扣

解法:递归三部曲

public boolean isBalanced(TreeNode root) {
    return getHeight(root) != -1;
}

private int getHeight(TreeNode root) {
    if (root == null) {
        return 0;
    }
    int leftNum = getHeight(root.left);
    if (leftNum == -1) {  // 左子树为-1表示已经不是平衡树了
        return -1;
    }
    int rightNum = getHeight(root.right);
    if (rightNum == -1) { // 右子树为-1表示已经不是平衡树了
        return -1;
    }
    // 左右子树高度差大于1,return -1表示已经不是平衡树了
    if (Math.abs(leftNum - rightNum) > 1) {
        return -1;
    }
    return Math.max(leftNum, rightNum) + 1;
}

Leetcode 257 力扣

题目需要视频理解递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径_哔哩哔哩_bilibili

解法一:返回树的所有路径中左右,用前序遍历,递归+回溯

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;

    }

    private void traversal(TreeNode root, List<Integer> path, List<String> res) {

        path.add(root.val); // 中

        if (root.left == null && root.right == null){ // 叶子节点收集结果

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < path.size()-1; i++) {

                sb.append(path.get(i)).append("->");

            }

            sb.append(path.get(path.size()-1)); // 收集最后一个节点,不需要->拼接

            res.add(sb.toString());

            return;

        }

        //path.add(root.val); // 中不能放在递归终止条件之后,会遗漏当前叶子节点val

        if(root.left != null){ // 左

            traversal(root.left,path,res); // 递归

            path.remove(path.size()-1); // 回溯

        }

        if(root.right != null){ // 右

            traversal(root.right,path,res); // 递归

            path.remove(path.size()-1); // 回溯

        }

    }

解法二:迭代使用栈

/**
 * 迭代法
 */
public List<String> binaryTreePaths1(TreeNode root) {
    List<String> result = new ArrayList<>();
    if (root == null)
        return result;
    Stack<Object> stack = new Stack<>();
    // 节点和路径同时入栈
    stack.push(root);
    stack.push(root.val + "");
    while (!stack.isEmpty()) {
        // 节点和路径同时出栈
        String path = (String) stack.pop();
        TreeNode node = (TreeNode) stack.pop();
        // 若找到叶子节点
        if (node.left == null && node.right == null) {
            result.add(path);
        }
        //右子节点不为空
        if (node.right != null) {
            stack.push(node.right);
            stack.push(path + "->" + node.right.val);
        }
        //左子节点不为空
        if (node.left != null) {
            stack.push(node.left);
            stack.push(path + "->" + node.left.val);
        }
    }
    return result;
}

Leetcode 404 力扣

解法:递归+后序遍历(要收集左子树和右子树的左叶子结点和)

public int sumOfLeftLeaves(TreeNode root) {
    if (root == null) {
        return 0;
    }
    int leftValue = 0; // 左
    if (root.left != null && root.left.left == null && root.left.right == null) {// 左子树就是一个左叶子的情况
        leftValue = root.left.val;
    } else {
        leftValue = sumOfLeftLeaves(root.left);
    }
    int rightValue = sumOfLeftLeaves(root.right); // 右

    return leftValue + rightValue; // 中
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值