题解 | #二叉树的最大深度#

二叉树的最大深度

https://www.nowcoder.com/practice/8a2b2bf6c19b4f23a9bdb9b233eefa73

package com.hhdd;

import com.hhdd.数据结构.树.TreeNode;

/**
 * @Author huanghedidi
 * @Date 2024/3/20 21:16
 */
public class 二叉树的最大深度 {
    static int res = 0;

    public static int maxDepth(TreeNode root) {

        int curDepth = 0;
        traverse(root, curDepth);
        return res;
        // write code here
    }

    public static void traverse(TreeNode node, int curDepth) {
        if (node == null) {
            return;
        }
        curDepth++;
        if (curDepth > res) {
            res = curDepth;
        }
        traverse(node.left, curDepth);
        traverse(node.right, curDepth);
    }

}

全部评论

相关推荐

05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务