-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum_depth.cpp
More file actions
139 lines (116 loc) · 3.21 KB
/
Copy pathmaximum_depth.cpp
File metadata and controls
139 lines (116 loc) · 3.21 KB
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
138
////////////////////////////////////////////////////////////////////////////////
//
// Maximum depth of binary tree
// from https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
//
// Description
// Given a binary tree, find its maximum depth.
//
// the maximum depth is the number of nodes along path from the root node down
// to the farthest leaf node.
//
// NOTE: A leaf is a node with no children.
//
// Example
// Given a binary tree [3, 9, 20, null, null, 15, 7]
// 3
// / \
// 9 20
// / \
// 15 7
//
// Solution
// 1) recursion
// 2) stack
// 3) dfs
//
////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
struct BinaryTreeNode {
int val;
BinaryTreeNode *left, *right;
BinaryTreeNode(int x) : val{x}, left{nullptr}, right{nullptr} {}
};
////////////////////////////////////////////////////////////////////////////////
// recursive solution
int max_depth(BinaryTreeNode *node)
{
if (!node) return 0;
int left_depth = max_depth(node->left);
int right_depth = max_depth(node->right);
return std::max(left_depth, right_depth) + 1;
}
////////////////////////////////////////////////////////////////////////////////
// without recursion using stack
int max_depth_1(BinaryTreeNode *node)
{
if (!node) return 0;
std::stack<BinaryTreeNode*> stack;
BinaryTreeNode *curr{nullptr}, *prev{nullptr};
int max_depth{0};
stack.push(node);
while (!stack.empty()) {
curr = stack.top();
if (!prev || prev->left == curr || prev->right == curr) {
if (curr->left) {
stack.push(curr->left);
} else if (curr->right) {
stack.push(curr->right);
}
} else if (curr->left == prev) {
if (curr->right)
stack.push(curr->right);
} else {
stack.pop();
}
prev = curr;
max_depth = (max_depth > stack.size() ? max_depth : stack.size());
}
return max_depth;
}
////////////////////////////////////////////////////////////////////////////////
// queue iterative solution
int max_depth_2(BinaryTreeNode *node)
{
if (!node) return 0;
std::queue<BinaryTreeNode*> q;
q.push(node);
int max_depth = 0;
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; ++i) {
BinaryTreeNode *n = q.front();
q.pop();
if (n->left) q.push(n->left);
if (n->right) q.push(n->right);
}
++max_depth;
}
return max_depth;
}
int main(int argc, char** argv)
{
// 3 | root
// / \ | / \
// 9 20 | n1 n2
// / \ | / \
// 15 7 | n3 n4
BinaryTreeNode *root = new BinaryTreeNode(3);
BinaryTreeNode *n1 = new BinaryTreeNode(9);
BinaryTreeNode *n2 = new BinaryTreeNode(20);
BinaryTreeNode *n3 = new BinaryTreeNode(15);
BinaryTreeNode *n4 = new BinaryTreeNode(7);
// BinaryTreeNode *n5 = new BinaryTreeNode(30); // add new node
root->left = n1;
root->right = n2;
n2->left = n3;
n2->right = n4;
// n3->left = n5; // add n5 to n3 as left child
std::cout << "max depth: " << max_depth(root) << std::endl;
std::cout << "max depth: " << max_depth_1(root) << std::endl;
std::cout << "max depth: " << max_depth_2(root) << std::endl;
return 0;
}