-
Notifications
You must be signed in to change notification settings - Fork 0
/
0111.cpp
114 lines (103 loc) · 2 KB
/
0111.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
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
/*
// 层序遍历 队列迭代
int minDepth(TreeNode* root)
{
if (root == NULL)
return 0;
int depth = 0;
deque<TreeNode*> que;
que.push_back(root);
while (!que.empty())
{
int size = que.size();
// 记录最小深度
depth++;
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop_front();
if (node->left == NULL && node->right == NULL)
return depth;
if (node->left != NULL)
que.push_back(node->left);
if (node->right != NULL)
que.push_back(node->right);
}
}
return depth;
}
// 递归 后序遍历 一般是求高度
int minDepth(TreeNode* root)
{
return getDepth(root);
}
int getDepth(TreeNode* root)
{
if (root == NULL)
return 0;
int leftDepth = getDepth(root->left);
int rightDepth = getDepth(root->right);
if (root->left == NULL && root->right != NULL)
return 1 + rightDepth;
else if (root->left != NULL && root->right == NULL)
return 1 + leftDepth;
else
return 1 + min(leftDepth, rightDepth);
}
*/
int ans;
int minDepth(TreeNode* root)
{
ans = INT_MAX;
if (root == NULL)
return 0;
getDepth(root, 1);
return ans;
}
// 先序遍历 递归
void getDepth(TreeNode* root, int depth)
{
if(root->left == NULL && root->right == NULL) // 中
ans = ans > depth ? depth : ans;
if (root->left != NULL) // 左
{
depth++;
getDepth(root->left, depth);
depth--;
}
if (root->right != NULL) // 右
{
depth++;
getDepth(root->right, depth);
depth--;
}
return;
}
};
int main()
{
TreeNode* root = new TreeNode(1);
TreeNode* node = root;
node->left = new TreeNode(2);
node->right = new TreeNode(3);
node->left->left = new TreeNode(4);
node->left->right = new TreeNode(5);
// node->right->right = new TreeNode(7);
Solution S;
S.minDepth(root);
return 0;
}