刷题训练day16 | 第六章 二叉树 part05

题目1:【二刷完成】

代码1:

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int result=0;
        Queue<TreeNode> queue = new LinkedList<>();
        if (root==null) return result;
        queue.add(root);
        while(!queue.isEmpty()){
            int a=queue.size();
            for (int i=0;i<a;i++){
                TreeNode temp = queue.poll();
                if (i==0) result=temp.val;
                if(temp.left!=null) queue.offer(temp.left);
                if(temp.right!=null) queue.offer(temp.right);
            }
        }
        return result;
    }
}
class Solution {
    private int Deep = -1;
    private int value = 0;
    public int findBottomLeftValue(TreeNode root) {
        value = root.val;
        findLeftValue(root,0);
        return value;
    }
    public void findLeftValue (TreeNode root,int deep){
        if (root==null) return;
        if (root.left==null&&root.right==null){
            if (deep > Deep){
                value = root.val;
                Deep = deep; 
            }
        }
        if (root.left!=null) findLeftValue(root.left,deep + 1);
        if (root.right!=null) findLeftValue(root.right,deep + 1);
    }
}

题目2:【二刷完成】

代码2:

class Solution {
    int result = 0;
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root==null) return false;
        targetSum -= root.val;
        if (root.left==null&&root.right==null){
            return targetSum==0;
        }
        boolean a = hasPathSum(root.left,targetSum);
        boolean b = hasPathSum(root.right,targetSum);
        return (a||b);
    }
}
class Solution {
    public boolean hasPathSum(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=hasPathSum(root.left,targetSum);
            if (left) return true;
        }
        //递归右子树
        if (root.right!=null){
            boolean right=hasPathSum(root.right,targetSum);
            if (right) return true;
        }
        //在左右子树递归过程中如果没有发现符合要求的路径则返回false
        return false;
    }
}

题目3:【二刷完成】

代码:

class Solution {
    List<List<Integer>> result = new LinkedList<List<Integer>>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        onePath(root,targetSum);
        return result;
    }
    void onePath(TreeNode node, int targetSum){
        if (node==null) return;
        path.add(node.val);
        targetSum -= node.val;
        if (node.left==null && node.right==null && targetSum==0) {
            result.add(new LinkedList<>(path));
        }
        onePath(node.left,targetSum);
        onePath(node.right,targetSum);
        path.removeLast();
    }
    
}

题目4:【二刷完成】

代码4:

class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(inorder.length==0 || postorder.length==0) return null;
        int a = inorder.length;
        int b = postorder.length;
        return findNode(inorder,0,a,postorder,0,b);
    }
    private TreeNode findNode(int[] inorder, int inStart, int inEnd, int[] postorder, int postStart, int postEnd){
        if (postStart==postEnd) return null;
        //选择左闭右开
        int index = 0; 
        for (int i=0 ;i<inEnd;i++){
            if(inorder[i]==postorder[postEnd-1]){
                index = i;
                break;
            }
        }
        int len = index-inStart;
        TreeNode root = new TreeNode(inorder[index]);
        root.left = findNode(inorder,inStart,index,postorder,postStart,postStart+len);
        root.right = findNode(inorder,index+1,inEnd,postorder,postStart+len,postEnd-1);//注意这些数值范围
        return root;
    }
}

题目5:【二刷完成】

代码5:

class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return findNode(preorder,0,preorder.length,inorder,0,inorder.length);
    } 
    private TreeNode findNode(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd){
        if (preStart==preEnd || inStart==inEnd) return null;
        int index=0;
        for (int i=0;i<inEnd;i++){
            if (inorder[i]==preorder[preStart]){
                index = i;
                break;
            }
        }
        TreeNode root = new TreeNode(inorder[index]);
        int len = index - inStart;
        root.left = findNode(preorder,preStart+1,preStart+1+len,inorder,inStart,index);
        root.right = findNode(preorder,preStart+1+len,preEnd,inorder,index+1,inEnd);
        return root;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值