-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaxDepthOfBT.java
32 lines (30 loc) · 952 Bytes
/
MaxDepthOfBT.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
import java.util.ArrayDeque;
public class MaxDepthOfBT {
public int maxDepth(TreeNode root) {
if (root == null)
return 0;
ArrayDeque<TreeNode> queue = new ArrayDeque<TreeNode>();
// corresponding depth of each node
ArrayDeque<Integer> depths = new ArrayDeque<Integer>();
queue.addLast(root);
depths.addLast(1);
int max = Integer.MIN_VALUE;
// BFS traverse
while (!queue.isEmpty()) {
TreeNode n = queue.removeFirst();
int d = depths.removeFirst();
if (n.left != null) {
queue.addLast(n.left);
depths.addLast(d+1);
}
if (n.right != null) {
queue.addLast(n.right);
depths.addLast(d+1);
}
if (n.left == null && n.right == null && max < d) {
max = d;
}
}
return max;
}
}