代码随想录刷题Day18 | 513. 找树左下角的值 | 112. 路径总和 | 106.从中序与后序遍历序列构造二叉树

代码随想录刷题Day18 | 513. 找树左下角的值 | 112. 路径总和 | 106.从中序与后序遍历序列构造二叉树

513. 找树左下角的值

题目:

给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。

假设二叉树中至少有一个节点。

思路:

最简单的办法是使用层序遍历,最后一层的第一个元素就是我们要找的元素

还可以使用递归的办法,我们按照前序遍历的方式,记录访问的深度和访问到该深度时的元素的值

代码:

class Solution {
    int maxDeep = -1;
    int value = -1;
    public int findBottomLeftValue(TreeNode root) {
        find(root, 0);
        return value;
    }
    public void find(TreeNode root, int deep){
        if(maxDeep < deep){
            value = root.val;
            maxDeep = deep;
        }
        if(root.left != null) find(root.left, deep + 1);
        if(root.right != null) find(root.right, deep + 1);
    }
}

112. 路径总和

题目:

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false

叶子节点 是指没有子节点的节点。

思路:

利用回溯的思想,记录下当前节点的和,然后判断当前节点是否是叶子节点,利用了前序遍历。

代码:

class Solution {
    int nodeSum = 0;
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null) return false;
        nodeSum += root.val;
        boolean left = false, right = false;
        if(root.left == null && root.right == null && nodeSum == targetSum){
            return true;
        }
        if(root.left != null){
            left = hasPathSum(root.left, targetSum);
            nodeSum -= root.left.val;
        }
        if(root.right != null){
            right = hasPathSum(root.right, targetSum);
            nodeSum -= root.right.val;
        }
        return left || right;
    }
}

113. 路径总和 II

题目:

给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。

叶子节点 是指没有子节点的节点。

思路:

与上一题类似,多记录了一个当前访问过的节点的数组。

代码:

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int nodeSum = 0;
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null) return result;
        pathSum01(root, targetSum);
        return result;
    }
    public void pathSum01(TreeNode root, int targetSum){
        nodeSum += root.val;
        path.add(root.val);
        
        if(root.left == null && root.right == null && nodeSum == targetSum){
            result.add(new ArrayList<>(path));
        }
        if(root.left != null){
            pathSum01(root.left, targetSum);
            nodeSum -= root.left.val;
            path.remove(path.size() - 1);
        }
        if(root.right != null){
            pathSum01(root.right, targetSum);
            nodeSum -= root.right.val;
            path.remove(path.size() - 1);
        }
    }
}

106.从中序与后序遍历序列构造二叉树

题目:

给定两个整数数组 inorderpostorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树

思路:

由中序和后续遍历的数组可以还原出一个完整的二叉树(具体略)

代码:

class Solution {
    Map<Integer, Integer> map = new HashMap<>();
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        for(int i = 0; i < inorder.length; i++){
            map.put(inorder[i], i);
        }
        return build(inorder, postorder, 0, inorder.length, 0, postorder.length);
    }
    public TreeNode build(int[] inorder, int[] postorder, int inStart, int inEnd, int postStart, int postEnd){
        if(inStart >= inEnd || postStart >= postEnd) return null;
        int rootIndex = map.get(postorder[postEnd - 1]);
        int lenLeft = rootIndex - inStart;
        TreeNode root = new TreeNode(inorder[rootIndex]);
        root.left = build(inorder, postorder, inStart, rootIndex, postStart, postStart + lenLeft);
        root.right = build(inorder, postorder, rootIndex + 1, inEnd, postStart + lenLeft, postEnd - 1);
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值