题解 | #二叉树的深度#
二叉树的深度
https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
#include <algorithm>
class Solution {
public:
int TreeDepth(TreeNode* pRoot) {
if (pRoot == NULL) {
return 0;
}
return max(TreeDepth(pRoot->left), TreeDepth(pRoot->right)) + 1;
}
};
递归求二叉树深度,其实等于求最大递归深度,递归求左右子树的最大深度加一

查看30道真题和解析