|
9 | 9 | import java.util.Map;
|
10 | 10 | import java.util.Queue;
|
11 | 11 |
|
12 |
| - |
13 | 12 | /**
|
14 | 13 | * 662. Maximum Width of Binary Tree
|
15 | 14 | *
|
|
72 | 71 | Note: Answer will in the range of 32-bit signed integer.
|
73 | 72 | */
|
74 | 73 | public class _662 {
|
75 |
| - /** |
76 |
| - * Use a map to store the node to value map, |
77 |
| - * we use root as index 1, then its left child is 2*i-1 and right child is 2*i |
78 |
| - */ |
79 |
| - public int widthOfBinaryTree(TreeNode root) { |
80 |
| - if (root == null) { |
81 |
| - return 0; |
82 |
| - } |
| 74 | + public static class Solution1 { |
| 75 | + /** |
| 76 | + * Use a map to store the node to value map, |
| 77 | + * we use root as index 1, then its left child is 2*i-1 and right child is 2*i |
| 78 | + */ |
| 79 | + public int widthOfBinaryTree(TreeNode root) { |
| 80 | + if (root == null) { |
| 81 | + return 0; |
| 82 | + } |
83 | 83 |
|
84 |
| - Queue<Map.Entry<TreeNode, Integer>> queue = new LinkedList<>(); |
85 |
| - queue.offer(new AbstractMap.SimpleEntry<>(root, 1)); |
86 |
| - int max = 1; |
87 |
| - while (!queue.isEmpty()) { |
88 |
| - int size = queue.size(); |
89 |
| - List<Map.Entry<TreeNode, Integer>> list = new ArrayList<>(); |
90 |
| - for (int i = 0; i < size; i++) { |
91 |
| - Map.Entry<TreeNode, Integer> curr = queue.poll(); |
92 |
| - if (curr.getKey().left != null) { |
93 |
| - Map.Entry<TreeNode, Integer> newEntry = new AbstractMap.SimpleEntry<>(curr.getKey().left, curr.getValue() * 2 - 1); |
94 |
| - queue.offer(newEntry); |
95 |
| - list.add(newEntry); |
| 84 | + Queue<Map.Entry<TreeNode, Integer>> queue = new LinkedList<>(); |
| 85 | + queue.offer(new AbstractMap.SimpleEntry<>(root, 1)); |
| 86 | + int max = 1; |
| 87 | + while (!queue.isEmpty()) { |
| 88 | + int size = queue.size(); |
| 89 | + List<Map.Entry<TreeNode, Integer>> list = new ArrayList<>(); |
| 90 | + for (int i = 0; i < size; i++) { |
| 91 | + Map.Entry<TreeNode, Integer> curr = queue.poll(); |
| 92 | + if (curr.getKey().left != null) { |
| 93 | + Map.Entry<TreeNode, Integer> newEntry = new AbstractMap.SimpleEntry<>(curr.getKey().left, curr.getValue() * 2 - 1); |
| 94 | + queue.offer(newEntry); |
| 95 | + list.add(newEntry); |
| 96 | + } |
| 97 | + if (curr.getKey().right != null) { |
| 98 | + Map.Entry<TreeNode, Integer> newEntry = new AbstractMap.SimpleEntry<>(curr.getKey().right, curr.getValue() * 2); |
| 99 | + queue.offer(newEntry); |
| 100 | + list.add(newEntry); |
| 101 | + } |
96 | 102 | }
|
97 |
| - if (curr.getKey().right != null) { |
98 |
| - Map.Entry<TreeNode, Integer> newEntry = new AbstractMap.SimpleEntry<>(curr.getKey().right, curr.getValue() * 2); |
99 |
| - queue.offer(newEntry); |
100 |
| - list.add(newEntry); |
| 103 | + if (list.size() > 1) { |
| 104 | + max = Math.max(list.get(list.size() - 1).getValue() - list.get(0).getValue() + 1, max); |
101 | 105 | }
|
102 | 106 | }
|
103 |
| - if (list.size() > 1) { |
104 |
| - max = Math.max(list.get(list.size() - 1).getValue() - list.get(0).getValue() + 1, max); |
105 |
| - } |
| 107 | + return max; |
106 | 108 | }
|
107 |
| - return max; |
108 | 109 | }
|
109 | 110 | }
|
0 commit comments