-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMaximumDepth.java
More file actions
52 lines (37 loc) · 1.33 KB
/
Copy pathMaximumDepth.java
File metadata and controls
52 lines (37 loc) · 1.33 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
49
50
51
52
class Node {
TreeNode next;
int depth;
public Node(TreeNode next, int depth) {
this.next = next;
this.depth = depth;
}
}
class Solution {
Queue<Node> queue = new LinkedList<>();
int maximum = Integer.MIN_VALUE;
public int maxDepth(TreeNode root) {
if (root == null){
return 0;
} else {
Node node = new Node(root, 1);
queue.add(node);
while (!queue.isEmpty()) {
Node current = queue.poll();
if (current.next.left != null) {
node = new Node(current.next.left, current.depth + 1);
queue.add(node);
}
if (current.next.right != null) {
node = new Node(current.next.right, current.depth + 1);
queue.add(node);
}
if ((current.next.left == null) && (current.next.right == null)) {
if (maximum < current.depth) {
maximum = current.depth;
}
}
}
return maximum;
}
}
}