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

二叉树的最大深度

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

  1. 递归
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    public int maxDepth (TreeNode root) {
        if(root == null){
            return 0;
        }
        return maxDeep(root);
    }
    public static int maxDeep(TreeNode root){
        if(root == null){
            return 0;
        }
        return Math.max(maxDeep(root.left),maxDeep(root.right))+1;
    }
}
全部评论

相关推荐

皮格吉:不,有的厂子面试无手撕,可以试试。都是一边学一边面。哪有真正准备好的时候,别放弃
无实习如何秋招上岸
点赞 评论 收藏
分享
10-17 09:06
门头沟学院 Java
LZStarV:(这居然能又刷到你)感觉真的就是岗位少了吧,项目很多27届这个点都差不多这个水准,只能说有点不赶趟了先慢慢投吧兄弟,啥都投一下总有机会的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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