-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0104-maximum-depth-of-binary-tree.cpp
107 lines (87 loc) · 2.84 KB
/
0104-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
/*
Given root of binary tree, return max depth (# nodes along longest path from root to leaf)
At every node, max depth is the max depth between its left & right children + 1
Time: O(n)
Space: O(n)
*/
/**
* 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) {
// return maxDepthRecur(root); // recursive DFS - preorder - easiest
// return maxDepthStkInorder(root); // iterative DFS - inorder
return maxDepthStkPreorder(root); // iterative DFS - preorder - easier
// return maxDepthQueueLevelorder(root); // iterative BFS - levelorder - easy
}
int maxDepthQueueLevelorder(TreeNode* root) {
if (root == NULL) return 0;
queue<TreeNode*> q;
int ans = 0, depth = 0;
q.push(root);
while (!q.empty())
{
int s = q.size();
for(int i = 0; i < s; i++)
{
root = q.front();
q.pop();
if (root->left) q.push(root->left);
if (root->right) q.push(root->right);
}
depth += 1;
ans = max(ans, depth);
}
return ans;
}
int maxDepthStkPreorder(TreeNode* root) {
if (root == NULL) return 0;
stack<pair<TreeNode*, int>> s;
int ans = 1, depth = 1;
s.push({root, depth});
while (!s.empty())
{
root = s.top().first;
depth = s.top().second;
ans = max(ans, depth);
s.pop();
if (root->left) s.push({root->left, depth + 1});
if (root->right) s.push({root->right, depth + 1});
}
return ans;
}
int maxDepthStkInorder(TreeNode* root) {
stack<pair<TreeNode*, int>> s;
int ans = 0, depth = 0;
while (root || !s.empty())
{
while (root != NULL)
{
s.push(make_pair(root, ++depth));
root = root->left;
}
root = s.top().first;
ans = max(ans, depth);
depth = s.top().second;
s.pop();
root = root->right;
}
return ans;
}
int maxDepthRecur(TreeNode* root) {
// recursive DFS
if (root == NULL) return 0;
// we should inc. the depth by 1 here
// and check for max in left and right subtrees
return 1 + max(maxDepthRecur(root->left), maxDepthRecur(root->right));
}
}