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

代码随想录算法训练营Day14

513.找树左下角的值

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

层序遍历

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        if(root==null){
            return 0;
        }
        Deque<TreeNode> myque=new LinkedList<>();
        myque.offer(root);
        int count=0;
        while(!myque.isEmpty()){
            count++;
            int len=myque.size();
            while(len>0){
                TreeNode cur=myque.poll();
                if(cur.left!=null){
                    myque.offer(cur.left);
                }
                if(cur.right!=null){
                    myque.offer(cur.right);
                }
                len--;
            }
        }
        int count1=1;
        myque.offer(root);
        while(count1<count){
            count1++;
            int len=myque.size();
            while(len>0){
                TreeNode cur=myque.poll();
                if(cur.left!=null){
                    myque.offer(cur.left);
                }
                if(cur.right!=null){
                    myque.offer(cur.right);
                }
                len--;
            }
        }
        TreeNode cur2=myque.poll();
        return cur2.val;
    }
}

112. 路径总和

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

前序递归

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root==null){
            return false;
        }
        List<Integer> path=new ArrayList<>();     
        return hasSum(root,path,targetSum);
    }
    public boolean hasSum(TreeNode root,List<Integer> path,int targetSum){
        path.add(root.val);
        if(root.left==null&&root.right==null){
            int sum=0;
            for(Integer i:path){
                sum+=i;
            }
            if(sum==targetSum)
            return true;
            return false;
        }
        if(root.left!=null){
            boolean has=hasSum(root.left,path,targetSum);
            if(has)
            return has;
            path.remove(path.size()-1);
        }
        if(root.right!=null){
            boolean has=hasSum(root.right,path,targetSum);
            if(has)
            return has;
            path.remove(path.size()-1);
        }
        return false;
    }

}

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

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

循环不变量,前序递归

class Solution {
    Map<Integer,Integer> inordermap;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
         inordermap=new HashMap<>();
        for(int i=0;i<inorder.length;i++){
            inordermap.put(inorder[i],i);
        }
        return fondNode(inorder,0,inorder.length,postorder,0,postorder.length);
    }
    public TreeNode fondNode(int[] inorder,int instart,int inend,int[]postorder,int poststart,int postend){
        if(instart>=inend||poststart>=postend){
            return null;
        }
        int inindex=inordermap.get(postorder[postend-1]);
        TreeNode root=new TreeNode(postorder[postend-1]);
        int leftLen=inindex-instart;
        root.left=fondNode(inorder,instart,inindex,postorder,poststart,poststart+leftLen);
        root.right=fondNode(inorder,inindex+1,inend,postorder,poststart+leftLen,postend-1);
        return root;
    }
}


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

相关文章:

  • 【C#】CacheManager:高效的 .NET 缓存管理库
  • PCL库简单NDT算法配准
  • mini-lsm通关笔记Week2Overview
  • SpringBoot中使用XXL-JOB实现灵活控制的分片处理方案
  • C++的类型转换
  • Redis: 主从复制读写分离环境搭建
  • 2024电脑视频剪辑软件全解析与推荐
  • Prompt:在AI时代,提问比答案更有价值
  • O2OA(翱途)服务器故障排查
  • 学习经验分享【38】YOLOv11解读——最新YOLO版本
  • linux文件编程_文件
  • 记录一次gRpc流式操作
  • 正则表达式的使用示例--Everything文件检索批量重命名工具
  • 使用 Python 实现图形学的辐射度算法
  • Flask-2
  • Gpt4.0最新保姆级教程开通升级
  • 如何使用 Python 读取数据量庞大的 excel 文件
  • PostgreSQL+MybatisPlus,设置逻辑删除字段后查询出现:操作符不存在: boolean = integer 错误
  • 【mmengine】配置器(config)(进阶)继承与导出,命令行修改配置
  • 鸿蒙开发(NEXT/API 12)【硬件(常见问题)】驱动开发服务
  • 3-3 AUTOSAR RTE 对SR Port的作用
  • 51单片机的智能停车场【proteus仿真+程序+报告+原理图+演示视频】
  • Ubuntu 手动安装 ollama
  • 音视频入门基础:FLV专题(9)——Script Tag简介
  • mysql迁移postgreSql windows 工具
  • SQL SERVER 从嫌弃存储到爱上存储过程我给存储过程开发了版本控制工具和远程调试功能...
  • 基于ESP8266使用OLED显示温湿度和时间
  • Jmeter常用函数、逻辑控制器
  • 025.Oracle_DBMS_job定时任务
  • 单片机在控制和自动化任务中的应用场景广泛