当前位置: 首页 > article >正文

代码随想录算法训练营Day15

654.最大二叉树

力扣题目链接:. - 力扣(LeetCode)

前序递归、循环不变量

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return findmax(nums,0,nums.length);
    }
    public TreeNode findmax(int[] nums,int leftindex,int rightindex){
        if(rightindex==leftindex){
            return null;
        }
        if(rightindex-leftindex==1){
            return new TreeNode(nums[leftindex]);
        }
        int max=nums[leftindex];
        int maxindex=leftindex;
        for(int i=leftindex;i<rightindex;i++){
            if(nums[i]>max){
                max=nums[i];
                maxindex=i;
            }
        }
        TreeNode root=new TreeNode(max);
        root.left=findmax(nums,leftindex,maxindex);
        root.right=findmax(nums,maxindex+1,rightindex);
        return root;
    }
}

617.合并二叉树

力扣题目链接:. - 力扣(LeetCode)​​​​​​

前序递归

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;
    }
}

700.二叉搜索树中的搜索

力扣题目链接:. - 力扣(LeetCode)

class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if(root==null){
            return null;
        }
        if(val>root.val){
            return searchBST(root.right,val);
        }
        if(val<root.val){
            return searchBST(root.left,val);
        }
        return root;
    }
}

98.验证二叉搜索树

力扣题目链接:. - 力扣(LeetCode)

中序递归

class Solution {
    public boolean isValidBST(TreeNode root) {
        List<Integer> res=new ArrayList<>();
        midorder(root,res);
        for(int i=0;i<res.size()-1;i++){
            if(res.get(i)>=res.get(i+1)){
                return false;
            }
        }
        return true;
    }
    public void midorder(TreeNode root,List<Integer> res){
        if(root==null){
            return;
        }
        midorder(root.left,res);
        res.add(root.val);
        midorder(root.right,res);
    }
}


http://www.kler.cn/news/325714.html

相关文章:

  • 【面试题】软件测试实习(含答案)
  • 828华为云征文|针对Flexus X实例云服务器的CPU和内存性能测评
  • Quill Editor 富文本编辑器的高度问题
  • SWAP、AquaCrop、FVCOM、Delft3D、SWAT、R+VIC、HSPF、HEC-HMS......
  • 云计算中过等保三级需要的网络安全设备及详细讲解
  • 可视化大屏
  • CTFshow-SSRF
  • JSP(Java Server Pages)基础使用二
  • 自制CANTool_DBC_Layout仿制_布局读取Signal(三)
  • 【ShuQiHere】AVL 树(AVL Tree):如何保持二叉搜索树的平衡性?
  • 重构长方法之提取方法
  • 9.26-9.29学习
  • 信息安全数学基础(21)高次同余式的解数及解法
  • 【C++题目】7.双指针_和为 s 的两个数字
  • Python | Leetcode Python题解之第447题回旋镖的数量
  • 【linux 多进程并发】linux进程状态与生命周期各阶段转换,进程状态查看分析,助力高性能优化
  • 【C++——文件操作】
  • Allen Institute for Artificial Intelligence (Ai2) 发布开源多模态语言模型 Molmo
  • Mixture-of-Experts (MoE): 条件计算的诞生与崛起【下篇】
  • 四十四、多云/混合云架构设计(安全与合规策略)
  • watchEffect工作原理
  • docker学习笔记(1.0)
  • 面经4——亚信
  • Visual Studio Code 高级使用技巧:插件推荐、调试技巧与工作流优化
  • 【HTML5】html5开篇基础(5)
  • 怎么屏蔽统计系统统计到的虚假ip
  • 【分布式微服务云原生】探索RPC:远程过程调用的奥秘与技术实现
  • 汽车信息安全 -- 再谈车规MCU的安全启动
  • 【小程序 - 大智慧】Expareser 组件渲染框架
  • CSS学习 - 常用属性