其他分享
首页 > 其他分享> > 剑指 Offer 55 - I. 二叉树的深度

剑指 Offer 55 - I. 二叉树的深度

作者:互联网

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;

        int nleft = maxDepth(root.left);
        int nright = maxDepth(root.right);
        return (nleft > nright) ? nleft+1 : nright+1;
    }
}

 

标签:TreeNode,Offer,55,int,二叉树,nleft,root,nright,maxDepth
来源: https://www.cnblogs.com/peanut-zh/p/14148953.html