-
Notifications
You must be signed in to change notification settings - Fork 0
/
111_minDepthOfBT.cpp
93 lines (77 loc) · 1.98 KB
/
111_minDepthOfBT.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
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
struct TreeNode {
TreeNode* left;
TreeNode* right;
int val;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void helper(TreeNode *root, int& min, int level)
{
if (NULL == root->left && NULL == root->right) {
if (min > level) {
min = level;
std::cout << root->val << " " << level << std::endl;
}
}
if (root->left) {
helper(root->left, min, level + 1);
}
if (root->right) {
helper(root->right, min, level + 1);
}
}
int minDepth(TreeNode *root)
{
if (NULL == root) return 0;
// dfs
int min = std::numeric_limits<int>::max();
helper(root, min, 1);
return min;
// bfs
// using namespace std;
// queue<TreeNode *> nodeQueue;
// nodeQueue.push(root);
// int count = 1;
// int levelSize = 1;
// while (0 != levelSize) {
// TreeNode *tmp = nodeQueue.front();
// nodeQueue.pop();
// levelSize--;
// if (NULL == tmp->left && NULL == tmp->right) {
// return count;
// }
// if (tmp->left) {
// nodeQueue.push(tmp->left);
// }
// if (tmp->right) {
// nodeQueue.push(tmp->right);
// }
// if (0 == levelSize) {
// if (!nodeQueue.empty()) {
// levelSize = nodeQueue.size();
// count++;
// }
// else {
// break;
// }
// }
// }
// return count;
}
int main(int argc, char const *argv[])
{
TreeNode a(0);
a.left = new TreeNode(1);
a.right = new TreeNode(2);
a.left->left = new TreeNode(3);
a.left->right = new TreeNode(4);
a.left->left->left = new TreeNode(7);
a.left->left->right = new TreeNode(8);
a.right->left = new TreeNode(5);
a.right->right = new TreeNode(6);
std::cout << "depth: " << minDepth(&a) << std::endl;
return 0;
}