/**
* 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 max = 0;
void dfs(TreeNode *root, int d){
if(!root){
if(d > max)
max = d;
return ;
}
dfs(root->left, d + 1);
dfs(root->right, d + 1);
}
int treeDepth(TreeNode* root) {
int d = 0;
dfs(root, d);
return max;
}
//取巧做法
// int treeDepth(TreeNode* root) {
// if(!root) return 0;
// return max(treeDepth(root->left), treeDepth(root->right)) + 1;
// }
};