/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型
*/
int maxHigh=0;
//深度优先遍历递归版本
void dfs(TreeNode*root,int res)
{
if(root==NULL)
return ;
if(root->left)
dfs(root->left, res+1);
if(root->right)
dfs(root->right, res+1);
if(root->left==NULL&&!root->right){
maxHigh=maxHigh>res?maxHigh:res;
}
}
int maxDepth(TreeNode* root) {
// write code here
dfs(root,1);
return maxHigh;
}
};
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型
*/
int maxHigh=0;
//深度优先遍历递归版本
void dfs(TreeNode*root,int res)
{
if(root==NULL)
return ;
if(root->left)
dfs(root->left, res+1);
if(root->right)
dfs(root->right, res+1);
if(root->left==NULL&&!root->right){
maxHigh=maxHigh>res?maxHigh:res;
}
}
int maxDepth(TreeNode* root) {
// write code here
dfs(root,1);
return maxHigh;
}
};