-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay37.java
More file actions
63 lines (54 loc) · 1.94 KB
/
Day37.java
File metadata and controls
63 lines (54 loc) · 1.94 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
53
54
55
56
57
58
59
60
61
62
63
import java.util.LinkedList;
import java.util.Queue;
class TreeNode {
int val;
TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}
public class BinaryTreeWidth {
public int maxWidth(TreeNode root) {
if (root == null) {
return 0;
}
int maxWidth = 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int levelSize = queue.size();
int leftmostIndex = -1; // Initialize leftmost index for the current level
int rightmostIndex = -1; // Initialize rightmost index for the current level
for (int i = 0; i < levelSize; i++) {
TreeNode current = queue.poll();
if (current != null) {
if (leftmostIndex == -1) {
leftmostIndex = i; // Update leftmost index for the current level
}
rightmostIndex = i; // Update rightmost index for the current level
queue.offer(current.left);
queue.offer(current.right);
} else {
queue.offer(null);
queue.offer(null);
}
}
if (leftmostIndex != -1) {
int currentWidth = rightmostIndex - leftmostIndex + 1;
maxWidth = Math.max(maxWidth, currentWidth);
}
}
return maxWidth;
}
public static void main(String[] args) {
BinaryTreeWidth solution = new BinaryTreeWidth();
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
int maxWidth = solution.maxWidth(root);
System.out.println("Maximum Width: " + maxWidth); // Output: 2
}
}