题解 | #二叉树的深度#

二叉树的深度

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

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param pRoot TreeNode类 
 * @return int整型
 */

int  depth(struct TreeNode* root,int dep){
    int depl=dep,depr=dep;
    if(root->left!=NULL) depl=depth(root->left,dep+1);
    if(root->right!=NULL) depr=depth(root->right,dep+1);
    if(depl>depr) dep=depl;
    else dep=depr;
    return dep;
}

int TreeDepth(struct TreeNode* pRoot ) {
    // write code here
    int h;
    if(pRoot==NULL) return 0;
    else h=depth(pRoot,1);
    return h;
}

全部评论

相关推荐

深夜书店vv:腾讯是这样的,去年很多走廊都加桌子当工区
点赞 评论 收藏
分享
酷酷我灵儿帅:这去不去和线不线下面说实话没啥关系
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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