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

找树左下角的值

看完题后的思路

f(root,深度)
if(为叶子节点&&深度最大)
加入结果;
返回
if(左不为空)
遍历左子树
if(右不为空)
遍历右子树

代码

// day18
    //|513.找树左下角的值
    int findBottomLeftValueRes=0;
    int  findBottomLeftValuemaxdepth=0;
    public int findBottomLeftValue(TreeNode root) {
        if (root==null){
            return -1;
        }
        findBottomLeftValue(root,1);
        return findBottomLeftValueRes;
    }

    public void findBottomLeftValue(TreeNode root,int depth){
        if(root==null){
            return;
        }
        if (root.left==null&&root.right==null&&depth>findBottomLeftValuemaxdepth){
            findBottomLeftValuemaxdepth=depth;
            findBottomLeftValueRes=root.val;
            return;
        }
        findBottomLeftValue(root.left,depth+1);
        findBottomLeftValue(root.right,depth+1);
        
    }

复杂度

在这里插入图片描述

收获

当终止条件为叶子节点时的写法

  if(root==null){// 加这一句是为了下面不用判断
            return;
        }
        if (root.left==null&&root.right==null&&depth>findBottomLeftValuemaxdepth){
            findBottomLeftValuemaxdepth=depth;
            findBottomLeftValueRes=root.val;
            return;
        }
        findBottomLeftValue(root.left,depth+1);
        findBottomLeftValue(root.right,depth+1);

三刷敲出来

112. 路径总和

看完题后的思路

回朔算法

  1. bool f(root,sum)
  2. 终止条件
    sum=sum-value;
    if(sume==0&&叶子节点){
    return true;
    }
  3. left=f(左,sum值)
    right=f(右,sum)

代码

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

复杂度

在这里插入图片描述

收获

113.路径总和ii

看完题后的思路

先加入节点的回溯

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

收获

三刷过

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

 public TreeNode buildTree(int[] inorder, int[] postorder) {
        TreeNode treeNode = buildTree(inorder, 0, inorder.length, postorder, 0, postorder.length);
        return treeNode;

    }
    public TreeNode buildTree(int[] inorder,int ileft,int iright,  int[] postorder,int pleft,int pright){
        if (ileft==iright){
            return null;
        }
        int rootVal=postorder[pright-1];
        TreeNode root = new TreeNode(rootVal);
        int mid = getByIterm(inorder, rootVal, ileft, iright);

        TreeNode left = buildTree(inorder, ileft, mid, postorder, pleft, pleft + mid-ileft);
        TreeNode right = buildTree(inorder, mid + 1, iright, postorder, pleft + mid - ileft ,pright - 1);
        root.left=left;
        root.right=right;
        return root;
    }

    public int getByIterm(int[] num,int target,int left,int right) {
        for (int i = left; i <right ; i++) {
            if (num[i]==target){
                return i;
            }
        }
        return -1;
    }

在这里插入图片描述

收获

三刷过一遍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弈师亦友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值