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

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

java 复制代码
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,更新为结果。
*/
相关推荐
AOwhisky4 小时前
下一代容器来了?Docker 宣布原生支持 WebAssembly
java·运维·docker·容器·rust·wasm
QXWZ_IA5 小时前
1库1图1批是什么?千寻位置公安地图数据体系详解
科技·算法·能源·媒体·交通物流·政务
c238566 小时前
Bug 猎手入门指南
c++·算法·bug
Reart6 小时前
Leetcode 213.打家劫舍2(内含闲谈,打劫真是技术活,好题,716)
后端·算法
云云只是个程序马喽6 小时前
海外短剧平台搭建方案:私有化源码系统选型|云微短剧系统技术架构拆解
java·php
Reart7 小时前
Leetcode 198.打家劫舍(716)
后端·算法
Jerry7 小时前
LeetCode 110. 平衡二叉树
算法
C137的本贾尼7 小时前
第七篇:消息队列(MQ)——就是个带存储的异步通信管道
java·开发语言·中间件
Flittly7 小时前
【AgentScope Java新手村系列】(19)多模态-图像音频视频
java·spring boot·spring
玖玥拾7 小时前
C++ 数据结构 八大基础排序算法专题
数据结构·c++·算法·排序算法