-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_depth_of_binary_tree.c
More file actions
48 lines (43 loc) · 1.02 KB
/
Copy pathminimum_depth_of_binary_tree.c
File metadata and controls
48 lines (43 loc) · 1.02 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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
#define MAX_NODES 50
int minDepth(struct TreeNode* root){
if(!root) {
return 0;
}
int depth_stack[MAX_NODES];
struct TreeNode* stack[MAX_NODES];
stack[0] = root;
depth_stack[0] = 1;
int top = 1;
int min_depth = INT_MAX;
while(top) {
--top;
struct TreeNode* node = stack[top];
int depth = depth_stack[top];
if(!node->left && !node->right) {
if(depth < min_depth) {
min_depth = depth;
}
}
else {
if(node->left) {
stack[top] = node->left;
depth_stack[top] = depth + 1;
++top;
}
if(node->right) {
stack[top] = node->right;
depth_stack[top] = depth + 1;
++top;
}
}
}
return min_depth;
}