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

513. 找树左下角的值

难度中等405

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

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

示例 1:

输入: root = [2,1,3]
输出: 1
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        //反向层次遍历
        Deque<TreeNode> queue = new LinkedList<>();
        TreeNode last = root;
        queue.offerLast(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i =0;i < size;i++){
                TreeNode tmp = queue.pollFirst();
                last = tmp;
                if(tmp.right != null){
                    queue.offerLast(tmp.right);
                }
                if(tmp.left != null){
                    queue.offerLast(tmp.left);
                }
            }
        }
        return last.val;

    }
}

112. 路径总和

难度简单1054

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

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

示例 1:

输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
输出:true
解释:等于目标和的根节点到叶节点路径如上图所示。
class Solution {
    int target ;
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null)return false;
        target = targetSum;
        return dfs(root,0);
    }
    public boolean dfs(TreeNode root,int count ){
        if(root == null){
            return false;
        }
        count += root.val;
        //System.out.println(count);
        if(count == target && root.left == null && root.right == null)return true;
        return dfs(root.left,count)||  dfs(root.right,count);
    }
}

113. 路径总和 II

难度中等867

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

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

示例 1:

输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
输出:[[5,4,11,2],[5,8,4,5]]
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    int target;
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null)return res;
        target = targetSum;
        List<Integer> list = new ArrayList<>();
        dfs(root,0,list);
        return res;
    }
    public void dfs(TreeNode root,int count ,List<Integer> list){
        if(root == null)return;
        list.add(root.val);
        count += root.val;
        if(count == target && root.left == null && root.right == null){
            res.add(new ArrayList<>(list));
        }
        dfs(root.left,count,list);
        dfs(root.right,count,list);
        list.remove(list.size()-1);
     }
}

105. 从前序与中序遍历序列构造二叉树

难度中等1796

给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。

示例 1:

输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
输出: [3,9,20,null,null,15,7]
class Solution {
    int[] pre ;
    int[] in;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        this.pre = preorder;
        this.in = inorder;
        int n = preorder.length-1;
        return dfs(0,n,0,n);
    }
    public TreeNode dfs(int l1,int r1,int l2 ,int r2){
        if(l1 > r1 || l2 > r2)return null;
        int index = 0;
        for(int i =l2;i <= r2;i++){
            if(in[i] == pre[l1]){
                index = i;
                break;
            }
        }
        TreeNode root = new TreeNode(in[index]);
        root.left = dfs(l1+1,l1+index-l2,l2,index-1);
        root.right = dfs(l1+index-l2+1,r1,index+1,r2);
        return root;

    }
}

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

难度中等882

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

示例 1:

输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出:[3,9,20,null,null,15,7]
class Solution {
    int[] order ;
    int[] post;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        this.order = inorder;
        this.post = postorder;
        int n = inorder.length-1;
        TreeNode root = dfs(0,n,0,n);
        return root;
    }
    public TreeNode dfs(int l1,int r1,int l2,int r2){
        if(l1 > r1 || l2 > r2)return null;
        int index = 0;
        for(int i =0;i < order.length;i++){
            if(order[i] == post[r2]){
                index = i;
                break;
            }
        }
        TreeNode root = new TreeNode(order[index]);
        root.left = dfs(l1,index-1,l2,l2+(index - l1 - 1));
        root.right = dfs(index+1,r1,l2+index-l1,r2-1);
        return root;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值