|
69 | 69 | public class _314 { |
70 | 70 | public List<List<Integer>> verticalOrder_using_treemap(TreeNode root) { |
71 | 71 | List<List<Integer>> result = new ArrayList(); |
72 | | - if(root == null) return result; |
| 72 | + if (root == null) return result; |
73 | 73 | Queue<TreeNode> bfsQ = new LinkedList(); |
74 | 74 | Queue<Integer> indexQ = new LinkedList(); |
75 | 75 | TreeMap<Integer, List<Integer>> map = new TreeMap(); |
76 | 76 | bfsQ.offer(root); |
77 | 77 | indexQ.offer(0);//we set the root as index 0, left will be negative, right will be positive |
78 | | - while(!bfsQ.isEmpty()){ |
| 78 | + while (!bfsQ.isEmpty()) { |
79 | 79 | int qSize = bfsQ.size(); |
80 | | - for(int i = 0; i < qSize; i++){ |
| 80 | + for (int i = 0; i < qSize; i++) { |
81 | 81 | TreeNode curr = bfsQ.poll(); |
82 | 82 | int index = indexQ.poll(); |
83 | | - if(map.containsKey(index)){ |
| 83 | + if (map.containsKey(index)) { |
84 | 84 | map.get(index).add(curr.val); |
85 | | - } else if(!map.containsKey(index)){ |
| 85 | + } else if (!map.containsKey(index)) { |
86 | 86 | List<Integer> list = new ArrayList(); |
87 | 87 | list.add(curr.val); |
88 | 88 | map.put(index, list); |
89 | 89 | } |
90 | | - if(curr.left != null){ |
| 90 | + if (curr.left != null) { |
91 | 91 | bfsQ.offer(curr.left); |
92 | | - indexQ.offer(index-1); |
| 92 | + indexQ.offer(index - 1); |
93 | 93 | } |
94 | | - if(curr.right != null){ |
| 94 | + if (curr.right != null) { |
95 | 95 | bfsQ.offer(curr.right); |
96 | | - indexQ.offer(index+1); |
| 96 | + indexQ.offer(index + 1); |
97 | 97 | } |
98 | 98 | } |
99 | 99 | } |
100 | | - for(int i : map.keySet()){ |
| 100 | + for (int i : map.keySet()) { |
101 | 101 | result.add(map.get(i)); |
102 | 102 | } |
103 | 103 | return result; |
104 | 104 | } |
105 | 105 |
|
106 | 106 | public List<List<Integer>> verticalOrder_using_hashmap(TreeNode root) { |
107 | 107 | List<List<Integer>> result = new ArrayList(); |
108 | | - if(root == null) return result; |
| 108 | + if (root == null) return result; |
109 | 109 | Queue<TreeNode> bfsQ = new LinkedList(); |
110 | 110 | Queue<Integer> indexQ = new LinkedList(); |
111 | 111 | HashMap<Integer, List<Integer>> map = new HashMap(); |
112 | 112 | bfsQ.offer(root); |
113 | 113 | indexQ.offer(0);//we set the root as index 0, left will be negative, right will be positive |
114 | 114 | int min = 0, max = 0; |
115 | | - while(!bfsQ.isEmpty()){ |
| 115 | + while (!bfsQ.isEmpty()) { |
116 | 116 | int qSize = bfsQ.size(); |
117 | | - for(int i = 0; i < qSize; i++){ |
| 117 | + for (int i = 0; i < qSize; i++) { |
118 | 118 | TreeNode curr = bfsQ.poll(); |
119 | 119 | int index = indexQ.poll(); |
120 | | - if(map.containsKey(index)){ |
| 120 | + if (map.containsKey(index)) { |
121 | 121 | map.get(index).add(curr.val); |
122 | | - } else if(!map.containsKey(index)){ |
| 122 | + } else if (!map.containsKey(index)) { |
123 | 123 | List<Integer> list = new ArrayList(); |
124 | 124 | list.add(curr.val); |
125 | 125 | map.put(index, list); |
126 | 126 | } |
127 | | - if(curr.left != null){ |
| 127 | + if (curr.left != null) { |
128 | 128 | bfsQ.offer(curr.left); |
129 | | - indexQ.offer(index-1); |
130 | | - min = Math.min(min, index-1); |
| 129 | + indexQ.offer(index - 1); |
| 130 | + min = Math.min(min, index - 1); |
131 | 131 | } |
132 | | - if(curr.right != null){ |
| 132 | + if (curr.right != null) { |
133 | 133 | bfsQ.offer(curr.right); |
134 | | - indexQ.offer(index+1); |
135 | | - max = Math.max(max, index+1); |
| 134 | + indexQ.offer(index + 1); |
| 135 | + max = Math.max(max, index + 1); |
136 | 136 | } |
137 | 137 | } |
138 | 138 | } |
139 | | - for(int i = min; i <= max; i++){ |
| 139 | + for (int i = min; i <= max; i++) { |
140 | 140 | result.add(map.get(i)); |
141 | 141 | } |
142 | 142 | return result; |
|
0 commit comments