-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path104. Maximum Depth of Binary Tree.cpp
138 lines (123 loc) · 3.32 KB
/
104. Maximum Depth of Binary Tree.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
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.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its depth = 3.
This problem can be done recursively and iteratively.
*/
/**
Recursive Solution
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if ( !root ) return 0;
int l_c = 0;
int r_c = 0;
l_c = maxDepth( root->left );
r_c = maxDepth( root->right );
if ( l_c > r_c )
{
return l_c + 1;
}
return r_c + 1;
}
};
/**
* Iterative Solution Using queue
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if ( !root ) return 0;
int count = 0;
queue<TreeNode*> temp;
temp.push( root );
while ( !temp.empty() )
{
count++;
int size = temp.size();
int t_count = 0;
while ( t_count < size )
{
t_count++;
root = temp.front();
temp.pop();
if ( root->left ) temp.push( root->left );
if ( root->right ) temp.push( root->right );
}
}
return count;
}
};
/**
* Using DFS, stack, different approach from queue
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if ( !root ) return 0;
int depth = 0;
stack<TreeNode*> Tree_S;
stack<int> tag;
TreeNode* curr = root;
while ( curr || !Tree_S.empty() )
{
while ( curr )
{
Tree_S.push( curr );
tag.push(0);
curr = curr->left;
}
if ( tag.top() == 1 )
{
depth = depth > Tree_S.size() ? depth : Tree_S.size();
tag.pop();
Tree_S.pop();
curr = nullptr;
}
else
{
curr = Tree_S.top();
curr = curr->right;
tag.pop();
tag.push(1);
}
}
return depth;
}
};