代码随想录算法训练营第十八天|513.找树左下角的值、112. 路径总和、113.路径总和ii、106.从中序与后序遍历序列构造二叉树、105.从前序与中序遍历序列构造二叉树

文章介绍了如何利用递归策略解决二叉树问题,包括找树的左下角值、路径总和以及从遍历序列构造二叉树的方法。通过递归遍历,动态更新最大深度来寻找左下角值;路径总和问题采用递归回溯,当路径和等于目标值时返回true;根据中序和后序遍历序列构造二叉树,利用哈希映射辅助定位节点位置。
摘要由CSDN通过智能技术生成

513.找树左下角的值

题目链接:513.找树左下角的值

思路:确定递归函数的参数和返回值,树的根节点和最大深度为参数,返回值void。当遇到叶子节点统计最大深度。递归过程中使用回溯。

class Solution {
    private int result = 0;
    private int maxDepth = -1;
    public int findBottomLeftValue(TreeNode root) {
        traversal(root,0);
        return result;
    }
    public void traversal(TreeNode root,int depth){
        if(root == null)return;
        if(root.left == null && root.right == null){
            if(depth > maxDepth){
                maxDepth = depth;
                result = root.val;
            }
        }
        if(root.left!=null){
            depth++;
            traversal(root.left,depth);
            depth--;
        }
        if(root.right!=null){
            depth++;
            traversal(root.right,depth);
            depth--;
        }
    }
}

112. 路径总和

题目链接:112. 路径总和

思路:递归函数参数为根节点和计数器记录二叉树一条边之和。当计数器减为0且遍历到叶子节点说明找到目标和。单层递归逻辑是当递归函数返回true时说明找到了合适的路径,立即返回。

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

113.路径总和ii(后续补充。。)

题目链接:113.路径总和ii

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

题目链接:106.从中序与后序遍历序列构造二叉树

思路:取后序数组最后一个元素作为节点元素。找到后序数组最后一个元素在中序数组的位置,作为切割点。切割中序数组,切成中序左数组和中序右数组 。切割后序数组,切成后序左数组和后序右数组。递归处理左区间和右区间。

class Solution {
    Map<Integer, Integer> map;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        map = new HashMap<>();
        for (int i = 0; i < inorder.length; i++) { 
            map.put(inorder[i], i);
        }

        return findNode(inorder,  0, inorder.length, postorder,0, postorder.length);  
    }

    public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
        if (inBegin >= inEnd || postBegin >= postEnd) { 
            return null;
        }
        int rootIndex = map.get(postorder[postEnd - 1]); 
        TreeNode root = new TreeNode(inorder[rootIndex]);  
        int lenOfLeft = rootIndex - inBegin;
        root.left = findNode(inorder, inBegin, rootIndex,
                            postorder, postBegin, postBegin + lenOfLeft);
        root.right = findNode(inorder, rootIndex + 1, inEnd,
                            postorder, postBegin + lenOfLeft, postEnd - 1);

        return root;
    }
}

105.从前序与中序遍历序列构造二叉树(后续补充。。)

题目链接:105.从前序与中序遍历序列构造二叉树

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值