-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmaximum_depth.cpp
61 lines (51 loc) · 1.02 KB
/
maximum_depth.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
51
52
53
54
55
56
57
58
59
60
61
/**
* @Author: Chacha
* @Date: 2019-02-16 13:26:00
* @Last Modified by: Chacha
* @Last Modified time: 2019-02-16 18:30:38
*/
#include <iostream>
#include <queue>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
int maxDepth(TreeNode *root)
{
if (root == NULL)
return 0;
queue<TreeNode *> q;
q.push(root);
int max_depth = 0;
while (!q.empty())
{
int size = q.size();
for (int i = 0; i < size; i++)
{
TreeNode *node = q.front();
q.pop();
if (node->left)
{
q.push(node->left);
}
if (node->right)
{
q.push(node->right);
}
}
++max_depth;
}
return max_depth;
}
};
int main()
{
return 0;
}