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

LeetCode 513. 找树左下角的值 java题解

https://leetcode.cn/problems/find-bottom-left-tree-value/description/

class Solution {
    int res=0;//初始值无所谓
    int max_depth=0;
    public int findBottomLeftValue(TreeNode root) {
        find(root,0);//
        return res;
    }
    public void find(TreeNode root,int depth){
        if(root==null) return;
        depth++;
        if(depth>max_depth){
            max_depth=depth;
            res=root.val;
        }
        find(root.left,depth);
        find(root.right,depth);
    }
}
/*
左中右,遍历节点。遍历过程中记录深度。
如果他的深度>max_depth,更新为结果。
*/

http://www.kler.cn/a/582622.html

相关文章:

  • Rabbitmq运维
  • 设计模式Python版 模板方法模式
  • 21 | 全面测试项目功能
  • python数据分析--pandas读取数据--按行和列提取数据
  • 【贪心算法4】
  • 人工智能在医疗领域的应用:技术革新与未来展望
  • 【解决报错】:detected dubious ownership in repository at ‘D:/idea_code/xxx‘问题
  • 【2步解决】phpstudy开机自启(自动启动phpstudy、mysql、nignx或apache、自动打开网址)
  • 【蓝桥杯python研究生组备赛】003 贪心
  • Python----计算机视觉处理(opencv:像素,RGB颜色,图像的存储,opencv安装,代码展示)
  • 动态规划-第2篇
  • JAVA SE 4.Java各版本特性
  • 手机端安装包签名原理
  • C++中数组作为函数参数传递,和使用指针作为函数参数有什么区别?
  • Webpack 打包详细教程
  • 推理模型对SQL理解能力的评测:DeepSeek r1、GPT-4o、Kimi k1.5和Claude 3.7 Sonnet
  • ELK traceId实现跨服务日志追踪
  • 传球游戏(蓝桥云课)
  • 附下载 | 2024 OWASP Top 10 基础设施安全风险.pdf
  • 【Godot4.0】EasyClock时钟组件