题解 | #二叉树的深度#

二叉树的深度

https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param pRoot TreeNode类 
 * @return int整型
 */
int TreeDepth(struct TreeNode* pRoot ) {
    // write code here
    if(pRoot == NULL) {
        return 0;
    }
    int ln = TreeDepth(pRoot->left);
    int rn = TreeDepth(pRoot->right);
    return ln>rn ? ln+1 : rn+1;    //返回值
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务