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

513. 这一题跟昨天的"404.左叶子之和" 有点像,有两种方法可以做,递归和层次遍历
递归:要找到最后一层 最左边的的叶子结点,首先肯定要递归一个深度然后不断的更新res的值最左边的结点,在遍历的时候以及隐形的找到了(这里有点不太理解,前中后遍历都行),反之递归就这么不懂的写下来了。
第二种方法层次遍历:层次遍历比较简单只要遍历到最后一层的第一个值就行了,i==0的时候。
递归:
class Solution {
    int Depth = -1; // 处理只有一个节点的情况
    int value = 0;
    public int findBottomLeftValue(TreeNode root) {
        dfs(root, 0);
        return value;
    }

    public void dfs(TreeNode root, int deep){
        if(root==null) return;

        //叶子结点
        if(root.left==null && root.right==null){
            if(deep > Depth){
                Depth = deep;
                value = root.val;
            }
        }

        if(root.left!=null) dfs(root.left, deep+1);
        if(root.right!=null) dfs(root.right, deep+1);
    }
}

层次遍历:

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        Deque<TreeNode> que = new LinkedList<>();
        que.offer(root);
        int res = 0;
        while(!que.isEmpty()){
            int size = que.size();
            for(int i=0; i<size; i++){
                TreeNode tmpNode = que.poll();
                if(i==0){//每一层的第一个结点,这里肯定就是最左边的的结点
                    res = tmpNode.val;
                }
                if(tmpNode.left != null) que.offer(tmpNode.left);
                if(tmpNode.right != null) que.offer(tmpNode.right);
            }
        }
        return res;
    }
}

112. 这题又是涉及回溯,跟之前的题目好像。虽然不是特别理解递归,但是好像已经找到这种题目的套路和奥秘了哈哈哈。精髓都在于判断叶子结点那儿块了

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        return dfs(root, targetSum);
    }

    public boolean dfs(TreeNode root, int targetSum){
        if(root==null) return false;

        targetSum -= root.val;
        //精髓
        if(root.left ==null && root.right==null) return targetSum==0;

        if(root.left != null){
            boolean left = dfs(root.left, targetSum);
            if(left) return true; //找到了
        }

        if(root.right != null){
            boolean right = dfs(root.right, targetSum);
            if(right) return true; //找到了
        }

        return false;
    }
}

113. 这一题跟之前的也都很相像,只是这个不需要返回值只可意会不可言传的题目,多做做就有感觉了,假装自己能理解了哈哈哈。

class Solution {
    List<List<Integer>> res =  new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<Integer> path =  new ArrayList<>();
        dfs(root, targetSum, path);
        return res;
    }

    public void dfs(TreeNode root, int targetSum, List<Integer> path){
        if(root==null) return;
        path.add(root.val);

        if(root.left ==null && root.right==null){
            if(targetSum - root.val == 0){ // 找到了和为 targetsum 的路径
                res.add(new ArrayList<>(path));//!
            }
            return;
        }

        if(root.left != null){
            dfs(root.left, targetSum - root.val, path);
            path.remove(path.size() - 1);//回溯
        }

        if(root.right != null){
            dfs(root.right, targetSum - root.val, path);
            path.remove(path.size() - 1);//回溯
        }

    }
}

106. 题目代码感觉不是特别难,主要是一个思路问题。看了carl大佬的视频讲解感觉理解了,但是你让我自己写代码还是写不出来,累了先复制粘帖吧,二刷有时间再来看,之前面试碰到过给一个数组然后构建二叉树的,就说下思路就行,这个代码写不出来我认了。。。106.从中序与后序遍历序列构造二叉树icon-default.png?t=N7T8https://www.bilibili.com/video/BV1vW4y1i7dn/?vd_source=8e2358f9b27440129acde1350d40c3cb

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保存中序序列的数值对应位置
            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.从前序与中序遍历序列构造二叉树。这道题目应该跟上面差不多,但是上面那题我没自己动手写,这题的话先放在这,下次二刷回来补上这两题!

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值