1. Problem Description
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
2. My solution
這個題寫的時候沒有IDE,直接在提交框裏寫了竟然1A。
class Solution {
private:
int ans;
public:
void DFS(TreeNode* root,int ceng)
{
if(!root)
return ;
if(ceng>ans)
ans=ceng;
DFS(root->left,ceng+1);
DFS(root->right,ceng+1);
}
int maxDepth(TreeNode* root) {
ans=0;
DFS(root,1);
return ans;
}
};