题目描述
思路分析
二叉树+dfs
代码实现
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
int res=0;
int TreeDepth(TreeNode* root) {
if(!root) return 0;
dfs(root,1);
return res;
}
void dfs(TreeNode* root,int depth){
if(!root->left&&!root->right){
res=max(res,depth);
return;
}
if(root->left) dfs(root->left,depth+1);
if(root->right) dfs(root->right,depth+1);
}
};