【二叉树part05】| 513.找树左下角的值、112.路径总和、113.路径总和||、106.从中序和后序遍历序列构造二叉树、105.从前序和中序遍历序列构造二叉树

目录

✿LeetCode513.找树左下角的值❀ 

✿LeetCode112.路径总和❀

✿LeetCode113.路径总和||❀

✿LeetCode106.从中序与后序遍历序列构造二叉树❀

 ✿LeetCode105.从前序与中序遍历构造二叉树❀


✿LeetCode513.找树左下角的值❀ 

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

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

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

 我的思路是直接层序遍历,然后返回最后一层的第一个元素,代码如下:

public int findBottomLeftValue(TreeNode root) {
        // 二叉树的节点个数的范围是 [1,104]
        Queue<TreeNode> qu=new LinkedList<>();
        List<List<Integer>> result=new ArrayList<>();
        qu.offer(root);
        while(!qu.isEmpty()){
            int size=qu.size();
            List<Integer> list=new ArrayList<>();
            while(size>0){
                TreeNode node=qu.poll();
                
                list.add(node.val);
                if(node.left!=null){
                    qu.offer(node.left);
                }
                if(node.right!=null){
                    qu.offer(node.right);
                }
                size--;
            }
            result.add(list);
        }
        return result.get(result.size()-1).get(0);
    }

✿LeetCode112.路径总和❀

链接:112.路径总和

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

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

求路径题典型的回溯法,代码如下: 

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

    }

✿LeetCode113.路径总和||❀

链接:113.路径总和||

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

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

还是回溯法,回溯法的精髓就是减去然后递归然后再加回来,代码如下:

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

✿LeetCode106.从中序与后序遍历序列构造二叉树❀

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

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

要知道中序遍历和后序遍历的顺序,然后在纸上自己构造一遍,根据自己的思路写出代码,要注意的点就是分割数组的时候保持一致性,要么是左闭右闭,要么是左闭右开,代码如下: 

public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(inorder.length==0 || postorder.length==0){
            return null;
        }
        return build(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
    }
    public TreeNode build(int[] inorder,int in_start,int in_end,int[] postorder,int post_start,int post_end){
         if (in_start > in_end || post_start > post_end) { 
            return null;
        }
        TreeNode root=new TreeNode(postorder[post_end]);
        int index=0;
        for(int i=in_start;i<=in_end;i++){
            if(inorder[i]==postorder[post_end]){
                index=i;
                break;
            }
        }
        int leftLen=index-in_start;
        root.left=build(inorder,in_start,in_start+leftLen-1,postorder,post_start,post_start+leftLen-1);
        root.right=build(inorder,in_start+leftLen+1,in_end,postorder,post_start+leftLen,post_end-1);
        return root;
    }

 ✿LeetCode105.从前序与中序遍历构造二叉树❀

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

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

如果会用中序与后序构造二叉树,那么前序与中序也是一样的,代码如下: 

public TreeNode buildTree(int[] preorder, int[] inorder) {
        return bulid(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
    }
    public TreeNode bulid(int[] preorder,int pre_start,int pre_end,int[] inorder,int in_start,int in_end){
        if(in_start>in_end || pre_start>pre_end){
            return null;
        }
        TreeNode root=new TreeNode(preorder[pre_start]);
        int index=0;
        for(int i=in_start;i<=in_end;i++){
            if(inorder[i]==preorder[pre_start]){
                index=i;
                break;
            }
        }
        int leftLen=index-in_start;
        root.left=bulid(preorder,pre_start+1,pre_start+leftLen,inorder,in_start,in_start+leftLen-1);
        root.right=bulid(preorder,pre_start+leftLen+1,pre_end,inorder,in_start+leftLen+1,in_end);
        return root;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值