Description:
题目大意:求二叉树的最大深度
解题思路:
算法标签:DFS
代码:
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
int ans;
void DFS(TreeNode* root,int index) {
if(root == NULL)
return;
if(index > ans)
ans = index;
DFS(root -> left , index + 1);
DFS(root -> right , index + 1);
}
int maxDepth(TreeNode* root) {
ans = 1;
if(root == NULL)
return 0;
DFS(root,1);
return ans;
}
};