代码随想录Day13

目录

1、513.找树左下角的值

2、112. 路径总和

3、113. 路径总和ii

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

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

6、654. 最大二叉树

7、617. 合并二叉树


1、513.找树左下角的值

层次遍历,easy

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> que = new LinkedList<>();
        que.add(root);
        int res = root.val;
        while(!que.isEmpty())
        {
            int size = que.size();
            for (int i = 0; i < size; ++i)
            {
                TreeNode tmp = que.poll();
                if(tmp.left != null) que.add(tmp.left);
                if(tmp.right != null) que.add(tmp.right);
                if(i == 0) res = tmp.val;
            }
        }
        return res;
    }
}

2、112. 路径总和

递归

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

3、113. 路径总和ii

递归

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) return res;
        List<Integer> pathList = new ArrayList<>();
        getPath(root,pathList,res,targetSum);
        return res;
    }
    public void getPath(TreeNode root,List<Integer> pathList,List<List<Integer>> res,int sum)
    {
        pathList.add(root.val);
        sum = sum - root.val;

        if(root.left == null && root.right == null)
        {
            if(sum == 0)
                res.add(new ArrayList<>(pathList));
            return;
        }
        
        if(root.left != null) 
        {
            getPath(root.left,pathList,res,sum);
            pathList.remove(pathList.size() - 1);
        }

        if(root.right != null) 
        {
            getPath(root.right,pathList,res,sum);
            pathList.remove(pathList.size() - 1);
        }
    }
}

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

递归

class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        int len = inorder.length;
        if (len == 1) return new TreeNode(inorder[0]);
        int root = postorder[len - 1];
        int root_index = 0;
        for(int i = 0; i < len; ++i)
        {
            if(inorder[i] == root) root_index = i;
        }
        TreeNode res = new TreeNode(root);
        if(root_index != 0) res.left = buildTree(Arrays.copyOfRange(inorder,0,root_index),Arrays.copyOfRange(postorder,0,root_index));
        if(root_index != len - 1) res.right = buildTree(Arrays.copyOfRange(inorder,root_index + 1,len),Arrays.copyOfRange(postorder,root_index,len - 1));
        return res;
    }  
}

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

递归

class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        int len = inorder.length;
        if (len == 1) return new TreeNode(inorder[0]);
        int root = preorder[0];
        int root_index = 0;
        for(int i = 0; i < len; ++i)
        {
            if(inorder[i] == root) root_index = i;
        }
        TreeNode res = new TreeNode(root);
        if(root_index != 0) res.left = buildTree(Arrays.copyOfRange(preorder,1,root_index + 1),Arrays.copyOfRange(inorder,0,root_index));
        if(root_index != len - 1) res.right = buildTree(Arrays.copyOfRange(preorder,root_index + 1,len),Arrays.copyOfRange(inorder,root_index + 1,len));
        return res;
    }
}

6、654. 最大二叉树

递归

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        int len = nums.length;
        if(len == 1) return new TreeNode(nums[0]);
        int root = nums[0];
        int root_index = 0;
        for(int i = 0; i < len; ++i)
        {
            if (nums[i] > root)
            {
                root = nums[i];
                root_index = i;
            }
        }
        TreeNode res = new TreeNode(root);
        if(root_index != 0) res.left = constructMaximumBinaryTree(Arrays.copyOfRange(nums,0,root_index));
        if(root_index != len - 1) res.right = constructMaximumBinaryTree(Arrays.copyOfRange(nums,root_index + 1,len));
        return res;
    }
}

7、617. 合并二叉树

递归

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1 == null) return root2;
        if(root2 == null) return root1;
        root1.val += root2.val;
        if(root1.left != null || root2.left != null) root1.left = mergeTrees(root1.left,root2.left);
        if(root1.right != null || root2.right != null) root1.right = mergeTrees(root1.right,root2.right);  
        return root1;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值