C++ 代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int treeDepth(TreeNode* root) {
if (root == NULL) return 0;
return max(treeDepth(root->left), treeDepth(root->right)) + 1;
}
};
/
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
/
class Solution {
int l;
int r;
public:
int treeDepth(TreeNode root) {
if (root == NULL) return 0;
l=1+treeDepth(root->left);
r=1+treeDepth(root->right);
return max(l,r);
};
我想问问我这个为啥过不去,问问
嗯,为什么要加上1
还要算上当前的父亲节点