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

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) {
        // write code here
        if (root == null) {
            return 0;
        }
        int leftmax = maxDepth(root.left);
        int rightmax = maxDepth(root.right);
        if (leftmax > rightmax) return leftmax + 1;
        else return rightmax + 1;
    }
}

全部评论

相关推荐

变异者:10元一天好笑程度100% 我去是哪个意思好笑程度1000%
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务