-
Notifications
You must be signed in to change notification settings - Fork 43
/
MinimumDepthOfBinaryTree.java
139 lines (113 loc) · 3.43 KB
/
MinimumDepthOfBinaryTree.java
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
package LeetCodeJava.Recursion;
// https://leetcode.com/problems/minimum-depth-of-binary-tree/
import LeetCodeJava.DataStructure.TreeNode;
import java.util.*;
/**
* The minimum depth is the number of nodes
* along the shortest path from the root node down to the nearest leaf node.
*
* -> NOTE !!! depth is node number, not distance
* > The minimum depth is the number of nodes ...
*
*/
public class MinimumDepthOfBinaryTree {
// V0
// NOTE !!! below is wrong
// public int minDepth(TreeNode root) {
//
// if (root == null){
// return 0;
// }
//
// int leftD = minDepth(root.left) + 1;
// int rightD = minDepth(root.right) + 1;
//
// return Math.min(leftD, rightD);
//
// }
// V0'
// IDEA : DFS
public int minDepth(TreeNode root) {
if (root == null){
return 0;
}
//maxDepth = Math.max(getDepth(root.left), getDepth(root.right));
return getDepth(root);
}
private int getDepth(TreeNode root){
if (root == null){
return 0;
}
// if (root.left != null){
// return 1 + getDepth(root.left);
// }
//
// if (root.right != null){
// return 1 + getDepth(root.right);
// }
/**
* NOTE !!! below condition
* -> we need to go till meat a node, then calculate min depths (number of node)
* -> Note: A leaf is a node with no children.
* -> plz check below example for idea
* example : [2,null,3,null,4,null,5,null,6]
*
*
*/
if (root.left == null) {
return 1 + getDepth(root.right);
} else if (root.right == null) {
return 1 + getDepth(root.left);
}
return 1 + Math.min(getDepth(root.left), getDepth(root.right));
}
// V1
// IDEA : DFS
// https://leetcode.com/problems/minimum-depth-of-binary-tree/editorial/
private int dfs(TreeNode root) {
if (root == null) {
return 0;
}
// If only one of child is non-null, then go into that recursion.
if (root.left == null) {
return 1 + dfs(root.right);
} else if (root.right == null) {
return 1 + dfs(root.left);
}
// Both children are non-null, hence call for both childs.
return 1 + Math.min(dfs(root.left), dfs(root.right));
}
public int minDepth_2(TreeNode root) {
return dfs(root);
}
// V2
// IDEA : BFS
// https://leetcode.com/problems/minimum-depth-of-binary-tree/editorial/
public int minDepth_3(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
int depth = 1;
while (q.isEmpty() == false) {
int qSize = q.size();
while (qSize > 0) {
qSize--;
TreeNode node = q.remove();
// Since we added nodes without checking null, we need to skip them here.
if (node == null) {
continue;
}
// The first leaf would be at minimum depth, hence return it.
if (node.left == null && node.right == null) {
return depth;
}
q.add(node.left);
q.add(node.right);
}
depth++;
}
return -1;
}
}