题解 | 二叉树的深度
二叉树的深度
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
int deepth,rdeepth,ldeepth;
if (pRoot == NULL) return 0;
rdeepth = TreeDepth(pRoot->right);
ldeepth = TreeDepth(pRoot->left);
deepth =ldeepth>rdeepth?ldeepth+1:rdeepth+1;
return deepth;
}
注意要进行深度+1,每次发现左右子节点有数据
