-
Notifications
You must be signed in to change notification settings - Fork 0
/
43_maximum-depth-of-binary-tree.cpp
112 lines (97 loc) · 2.67 KB
/
43_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
// DATE: 06-Aug-2023
/* PROGRAM: 43_Tree - Maximum Depth of Binary Tree
https://leetcode.com/problems/maximum-depth-of-binary-tree/
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down
to the farthest leaf node.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 3
Example 2:
Input: root = [1,null,2]
Output: 2
*/
// @ankitsamaddar @Aug_2023
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
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:
/*
// SOL1: RECURSIVE
int maxDepth(TreeNode* root) {
if (root == NULL) {
return 0;
}
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
*/
/*
// SOL2: BREADTH FIRST SEARCH
int maxDepth(TreeNode *root) {
if (root == NULL) {
return 0;
}
int level = 0;
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
level++;
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode *node = q.front();
q.pop();
if (node->left != NULL) {
q.push(node->left);
}
if (node->right != NULL) {
q.push(node->right);
}
}
}
return level;
}
*/
// SOL3: Depth First Search
int maxDepth(TreeNode *root) {
if (root == NULL) {
return 0;
}
int level = 0, res = 0;
stack<pair<TreeNode *, int>> sT;
sT.push({root, 1});
while (!sT.empty()) {
TreeNode *node = sT.top().first;
level = sT.top().second;
sT.pop();
res = max(res, level);
if (node->right != NULL) {
sT.push(make_pair(node->right, level + 1));
}
if (node->left != NULL) {
sT.push(make_pair(node->left, level + 1));
}
}
return res;
}
};
int main() {
TreeNode *root = new TreeNode(3);
root->left = new TreeNode(9);
root->right = new TreeNode(20);
root->right->left = new TreeNode(15);
root->right->right = new TreeNode(7);
Solution sol;
cout << sol.maxDepth(root) << endl; // expected output: 3
return 0;
}