代码随想录算法训练营DAY19 | 二叉树 (6)

一、LeetCode 654 最大二叉树

题目链接:654.最大二叉树icon-default.png?t=N7T8https://leetcode.cn/problems/maximum-binary-tree/

思路:坚持左开右闭原则,递归划分数组元素生成左右子树。

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return travel(nums,0,nums.length);
    }
    //坚持左闭右开
    public TreeNode travel(int[] nums, int left, int right){
        //空数组,返回空值
        if(right - left < 1){
            return null;
        }
        //数组只有一个元素,为叶子节点
        if(right - left == 1){
            return new TreeNode(nums[left]);
        }
        //找到数组中的最大元素及其下标
        int maxIndex = left;
        int maxValue = nums[left];
        for(int i = left+1; i < right; i++){
            if(nums[i] > maxValue){
                maxIndex = i;
                maxValue = nums[i];
            }
        }
        TreeNode node = new TreeNode(nums[maxIndex]);
        //划分数组生成左右子树
        node.left = travel(nums,left,maxIndex);
        node.right = travel(nums,maxIndex+1,right);
        return node;
    }
}
/**
 * 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;
 *     }
 * }
 */

 二、LeetCode 617 合并二叉树

题目链接:617.合并二叉树icon-default.png?t=N7T8https://leetcode.cn/problems/merge-two-binary-trees/submissions/502582353/

思路:前序递归遍历,处理空节点情况~

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        //处理root1和root2节点为空的情况
        if(root1 == null && root2 == null){
            return null;
        }
        if(root1 != null && root2 == null){
            return root1;
        }
        if(root1 == null && root2 != null){
            return root2;
        }
        //建立新节点
        TreeNode root = new TreeNode(root1.val + root2.val);
        //中、左、右递归遍历
        root.left = mergeTrees(root1.left,root2.left);
        root.right = mergeTrees(root1.right,root2.right);
        return root;
    }
}
/**
 * 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;
 *     }
 * }
 */

三、LeetCode  700 二叉树中的搜索

题目链接:700.二叉树中的搜索icon-default.png?t=N7T8https://leetcode.cn/problems/search-in-a-binary-search-tree/

思路:前序遍历,非左即右~

class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        //找到空节点,说明该路径上没有符合条件的节点
        if(root == null){
            return null;
        }
        //找到符合条件的节点
        if(root.val == val){
            return root;
        }
        //前序遍历 中、左、右
        TreeNode left = searchBST(root.left,val);
        TreeNode right = searchBST(root.right,val);
        //非左即右
        return  left == null ? right : left;
    }
}
/**
 * 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;
 *     }
 * }
 */

四、小结

        静心刷题ovo

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙南花已开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值