**Pre-order by Recursion [Accpeted]**

**Intuition**

- For solving this problem. we need a Pre-order to traversal the binary tree.
- We need to make a record of depth when traversal the binary tree.
- Each time traversal in leaf node, read the depth and store the bigger one.
- At last, we can get the result of maximum depth.

**Algorithm**

- Use the Pre-order algorithm.
- Use
to recode depth constantly.*temp* - Determine whether the current node is a leaf node. If yes and
bigger than*temp*(result's initial value is 0), do*result*.*result=temp*

**C**

```
int temp;
int result;
void PreOrder(struct TreeNode* root)
{
if(root==NULL) return;
temp++;
if(root->left==NULL&&root->right==NULL)
{
if(result<temp)
result=temp;
}
PreOrder(root->left);
PreOrder(root->right);
temp--;
}
int maxDepth(struct TreeNode* root) {
result=0;
temp=0;
PreOrder(root);
return result;
}
```

**Complexity Analysis**

- Time complexity: O(n)

Just like a pre-order traversal. - Space complexity: Who can tell me the space complexity?