-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode104.cpp
50 lines (48 loc) · 1.14 KB
/
leetcode104.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*************************************************************************
> File Name: leetcode104.cpp
> Author:
> Mail:
> Created Time: Mon 19 Sep 2016 12:43:15 AM PDT
************************************************************************/
#include<iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int maxDepth(TreeNode* root) {
if(root == NULL)
{
return 0;
}
int l = maxDepth(root->left);
int r = maxDepth(root->right);
if(l == 0)
{
return r + 1;
}
if(r == 0)
{
return l + 1;
}
return l < r ? r + 1:l + 1;
}
int main()
{
TreeNode* root = new TreeNode(1);
TreeNode* first = new TreeNode(1);
TreeNode* sec = new TreeNode(1);
TreeNode* third = new TreeNode(1);
TreeNode* forth = new TreeNode(1);
TreeNode* five = new TreeNode(1);
TreeNode* six = new TreeNode(1);
root->left = first;
root->right = sec;
sec->right = third;
third->left = forth;
forth->left = five;
first->left = six;
cout << maxDepth(root) << endl;
}