力扣404、513、654、617、700-java刷题笔记

本文介绍了五个与二叉树相关的LeetCode问题,包括计算左叶子之和、找树左下角的值、构建最大二叉树、合并二叉树以及在BST中搜索特定值。每个问题都提供了递归和迭代两种解决方案。
摘要由CSDN通过智能技术生成

一、404. 左叶子之和 - 力扣(LeetCode)

1.1题目

给定二叉树的根节点 root ,返回所有左叶子之和。

示例 1:

输入: root = [3,9,20,null,null,15,7] 
输出: 24 
解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24

示例 2:

输入: root = [1]
输出: 0

1.2思路分析

判断是否为左叶子的方法为:一个节点的左节点不为空且该节点的左节点的左节点为空且该节点的左节点的右节点为空,则该节点的左节点为左叶子节点。

1.3代码实现

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null ) return 0;
        if(root.left == null && root.right == null) return 0;
        int leftvale = sumOfLeftLeaves(root.left);
        if(root.left != null && root.left.left == null && root.left.right == null){
            leftvale = root.left.val;
        }
        int rightvale = sumOfLeftLeaves(root.right);
        int sum = rightvale + leftvale;
        return sum;

    }
}

二、513. 找树左下角的值 - 力扣(LeetCode)

2.1题目

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

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

示例 1:

输入: root = [2,1,3]
输出: 1

示例 2:

输入: [1,2,3,4,null,5,6,null,null,7]
输出: 7

2.2思路分析

注意是找树左下角的值不是左孩子,也就是求最后一层第一个值

方法一递归

方法二迭代:层序遍历

2.3代码实现

方法一递归

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    // 定义全局变量记录树的深度与最后一层第一个值
    // 递归
    int value = 0;
    int Deep = 0;
    public int findBottomLeftValue(TreeNode root) {
        value  = root.val;
        fun(root,0);
        return value;


    }
    public void fun(TreeNode root , int deep){
        if (root == null) return;
        if(root.left == null && root.right == null){
            if(deep>Deep){
                Deep = deep;
                value = root.val;
            }

        }
        // 回溯过程隐藏再参数传递中:deep+1
        if(root.left  != null )  fun(root.left,deep+1);
        if(root.right  != null ) fun(root.right,deep+1);
    }
}

方法二迭代:层序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
//  层序遍历
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int deep = 0;
        int result = 0;
        while(!queue.isEmpty()){
            deep = queue.size();
            for(int i = 0 ; i < deep; 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;
    }
}

三、654. 最大二叉树 - 力扣(LeetCode)

3.1题目

给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:

  1. 创建一个根节点,其值为 nums 中的最大值。
  2. 递归地在最大值 左边 的 子数组前缀上 构建左子树。
  3. 递归地在最大值 右边 的 子数组后缀上 构建右子树。

返回 nums 构建的 最大二叉树 

示例 1:

输入:nums = [3,2,1,6,0,5]
输出:[6,3,5,null,2,0,null,null,1]
解释:递归调用如下所示:
- [3,2,1,6,0,5] 中的最大值是 6 ,左边部分是 [3,2,1] ,右边部分是 [0,5] 。
    - [3,2,1] 中的最大值是 3 ,左边部分是 [] ,右边部分是 [2,1] 。
        - 空数组,无子节点。
        - [2,1] 中的最大值是 2 ,左边部分是 [] ,右边部分是 [1] 。
            - 空数组,无子节点。
            - 只有一个元素,所以子节点是一个值为 1 的节点。
    - [0,5] 中的最大值是 5 ,左边部分是 [0] ,右边部分是 [] 。
        - 只有一个元素,所以子节点是一个值为 0 的节点。
        - 空数组,无子节点。

示例 2:

输入:nums = [3,2,1]
输出:[3,null,2,null,1]

3.2思路分析 

首先找到数组中的最大值记录该值的下标并将该值作为根节点,然后根据记录的下标对数组进行划分,划分为用于遍历左数与右数的数组,然后再根据划分后的数组进行递归。

3.3代码实现

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return fun(nums,0,nums.length);
    }
    public TreeNode fun(int[] nums, int leftIndex, int rightIndex){
        // 空值判断
        if(rightIndex - leftIndex < 1) return null;
        // 只由一个值时
        if(rightIndex - leftIndex == 1) return new TreeNode(nums[leftIndex]);
        // 遍历数组找出最大值
        int index = leftIndex;
        int maxValue = nums[index];
        for(int i = leftIndex+1 ; i < rightIndex ;i++){
            if(nums[i]>maxValue){
                index = i;
                maxValue = nums[i];
            }
        }
        // 构造根节点
        TreeNode root = new TreeNode(maxValue);
        root.left = fun(nums,leftIndex,index);
        root.right = fun(nums, index+1, rightIndex);
        return root;


    }
}

四、617. 合并二叉树 - 力扣(LeetCode) 

4.1题目

给你两棵二叉树: root1 和 root2 。

想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。

返回合并后的二叉树。

注意: 合并过程必须从两个树的根节点开始。

示例 1:

输入:root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
输出:[3,4,5,5,4,null,7]

示例 2:

输入:root1 = [1], root2 = [1,2]
输出:[2,2]

提示:

  • 两棵树中的节点数目在范围 [0, 2000] 内
  • -104 <= Node.val <= 104

4.2思路分析

对两个二叉树一起遍历,对应位置相加即可

方法一递归

方法二迭代

4.3 代码实现

方法一递归

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        // 递归终止条件
        if(root1 == null) return root2;
        if(root2 == null) return root1;
        root1.val += root2.val;
        root1.left = mergeTrees(root1.left,root2.left);
        root1.right = mergeTrees(root1.right,root2.right);
        return root1;
    }
}

方法二迭代

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1 == null) return root2;
        if(root2 == null) return root1;
        Stack <TreeNode> stack = new Stack<>();
        stack.push(root2);
        stack.push(root1);
        while(!stack.isEmpty()){
            TreeNode temp1 = stack.pop();
            TreeNode temp2 = stack.pop();
            temp1.val += temp2.val;
            if(temp1.left!=null && temp2.left!=null){
               
                stack.push(temp2.left);
                stack.push(temp1.left);
            }else{
                if(temp2.left != null){
                    temp1.left = temp2.left;
                }
            }
            if(temp1.right!=null && temp2.right!=null){
                stack.push(temp2.right);
                stack.push(temp1.right);
               
            }else{
                if(temp2.right != null){
                    temp1.right = temp2.right;
                }
            }
        }
        return root1;

    }
}

五、700. 二叉搜索树中的搜索 - 力扣(LeetCode)

5.1题目 

给定二叉搜索树(BST)的根节点 root 和一个整数值 val

你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。

示例 1:

输入:root = [4,2,7,1,3], val = 2
输出:[2,1,3]

示例 2:

输入:root = [4,2,7,1,3], val = 5
输出:[]

提示:

  • 树中节点数在 [1, 5000] 范围内
  • 1 <= Node.val <= 107
  • root 是二叉搜索树
  • 1 <= val <= 107

5.2思路分析 

方法一递归

方法二迭代

5.3代码实现

方法一递归

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
//  递归
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if(root == null || root.val == val) return root;
        TreeNode temp = null;
        if(root.val>val) temp = searchBST(root.left,val);
        if(root.val<val) temp = searchBST(root.right,val);
        return temp;
    }
}

方法二迭代

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
//  迭代
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        while(root!=null){
            if(root.val>val) root = root.left;
            else if(root.val<val) root = root.right;
            else return root;
        }
        return null;

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值