/
btree.cpp
160 lines (127 loc) · 3.05 KB
/
btree.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <thread>
#include <mutex>
#include <condition_variable>
using namespace std;
template <typename T>
void print_vector(vector<T> const &v)
{
cout << "[";
for (auto it = v.begin(); it != v.end(); it++) {
if (it != v.begin())
cout << ", ";
cout << *it;
}
cout << "]";
}
template <typename T>
void print_vector2(vector<vector<T>> const &v)
{
cout << "[";
for (auto it = v.begin(); it != v.end(); it++) {
if (it != v.begin())
cout << ", ";
print_vector(*it);
}
cout << "]" << endl;
}
/* ListNode */
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
void print_ListNode(ListNode* h)
{
cout << "[";
if (h != nullptr) {
cout << h->val;
for (ListNode *it = h->next; it != nullptr; it = it->next)
cout << " ," << it->val;
}
cout << "]";
}
bool list_comp(ListNode *l1, ListNode *l2, ListNode *end) {
while (l1 != end) {
if (l1->val != l2->val)
return false;
l1 = l1->next;
l2 = l2->next;
}
return true;
}
ListNode* reverseList(ListNode* head) {
ListNode *prev = nullptr;
ListNode *next;
while (head != nullptr) {
next = head->next;
head->next = prev;
prev = head;
head = next;
}
return prev;
}
/* TreeNode */
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) {}
};
string &print_TreeNode1(string &s, TreeNode* t)
{
if (t == nullptr)
return s;
s += to_string(t->val);
s += ", ";
if (t->left)
print_TreeNode1(s, t->left);
if (t->right)
print_TreeNode1(s, t->right);
return s;
}
void print_TreeNode(TreeNode* t)
{
cout << "[";
string s = "";
s = print_TreeNode1(s, t);
cout << s.substr(0, s.size() - 2) << "]" << endl;
}
class Solution {
private:
public:
int minDepth(TreeNode* root) {
if (root == nullptr)
return 0;
if (root->left == nullptr && root->right == nullptr)
return 1;
if (root->left == nullptr)
return minDepth(root->right) + 1;
if (root->right == nullptr)
return minDepth(root->left) + 1;
int m1 = minDepth(root->left);
int m2 = minDepth(root->right);
if (m1 < m2)
return m1 + 1;
return m2 + 1;
}
};
int main() {
Solution s;
TreeNode *root = new TreeNode(3,
new TreeNode(9),
new TreeNode(20,
new TreeNode(15),
new TreeNode(7)));
int depth = s.minDepth(root);
cout << "Output: " << depth << endl;
return 0;
}