|
9 | 9 | import java.util.Queue;
|
10 | 10 | import java.util.TreeMap;
|
11 | 11 |
|
12 |
| - |
13 |
| -/**Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). |
| 12 | +/** |
| 13 | + * 314. Binary Tree Vertical Order Traversal |
| 14 | + * |
| 15 | + * Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). |
14 | 16 |
|
15 | 17 | If two nodes are in the same row and column, the order should be from left to right.
|
16 | 18 |
|
|
31 | 33 | [20],
|
32 | 34 | [7]
|
33 | 35 | ]
|
| 36 | +
|
34 | 37 | Given binary tree [3,9,8,4,0,1,7],
|
35 | 38 | 3
|
36 | 39 | /\
|
|
47 | 50 | [8],
|
48 | 51 | [7]
|
49 | 52 | ]
|
| 53 | +
|
50 | 54 | Given binary tree [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5),
|
51 | 55 | 3
|
52 | 56 | /\
|
|
65 | 69 | [3,0,1],
|
66 | 70 | [8,2],
|
67 | 71 | [7]
|
68 |
| - ]*/ |
| 72 | + ] |
| 73 | + */ |
69 | 74 | public class _314 {
|
70 |
| - public List<List<Integer>> verticalOrder_using_treemap(TreeNode root) { |
71 |
| - List<List<Integer>> result = new ArrayList(); |
72 |
| - if (root == null) { |
73 |
| - return result; |
74 |
| - } |
75 |
| - Queue<TreeNode> bfsQ = new LinkedList(); |
76 |
| - Queue<Integer> indexQ = new LinkedList(); |
77 |
| - TreeMap<Integer, List<Integer>> map = new TreeMap(); |
78 |
| - bfsQ.offer(root); |
79 |
| - indexQ.offer(0);//we set the root as index 0, left will be negative, right will be positive |
80 |
| - while (!bfsQ.isEmpty()) { |
81 |
| - int qSize = bfsQ.size(); |
82 |
| - for (int i = 0; i < qSize; i++) { |
83 |
| - TreeNode curr = bfsQ.poll(); |
84 |
| - int index = indexQ.poll(); |
85 |
| - if (map.containsKey(index)) { |
86 |
| - map.get(index).add(curr.val); |
87 |
| - } else if (!map.containsKey(index)) { |
88 |
| - List<Integer> list = new ArrayList(); |
89 |
| - list.add(curr.val); |
90 |
| - map.put(index, list); |
91 |
| - } |
92 |
| - if (curr.left != null) { |
93 |
| - bfsQ.offer(curr.left); |
94 |
| - indexQ.offer(index - 1); |
95 |
| - } |
96 |
| - if (curr.right != null) { |
97 |
| - bfsQ.offer(curr.right); |
98 |
| - indexQ.offer(index + 1); |
| 75 | + public static class Solution1 { |
| 76 | + public List<List<Integer>> verticalOrder(TreeNode root) { |
| 77 | + List<List<Integer>> result = new ArrayList(); |
| 78 | + if (root == null) { |
| 79 | + return result; |
| 80 | + } |
| 81 | + Queue<TreeNode> bfsQ = new LinkedList(); |
| 82 | + Queue<Integer> indexQ = new LinkedList(); |
| 83 | + TreeMap<Integer, List<Integer>> map = new TreeMap(); |
| 84 | + bfsQ.offer(root); |
| 85 | + indexQ.offer( |
| 86 | + 0);//we set the root as index 0, left will be negative, right will be positive |
| 87 | + while (!bfsQ.isEmpty()) { |
| 88 | + int qSize = bfsQ.size(); |
| 89 | + for (int i = 0; i < qSize; i++) { |
| 90 | + TreeNode curr = bfsQ.poll(); |
| 91 | + int index = indexQ.poll(); |
| 92 | + if (map.containsKey(index)) { |
| 93 | + map.get(index).add(curr.val); |
| 94 | + } else if (!map.containsKey(index)) { |
| 95 | + List<Integer> list = new ArrayList(); |
| 96 | + list.add(curr.val); |
| 97 | + map.put(index, list); |
| 98 | + } |
| 99 | + if (curr.left != null) { |
| 100 | + bfsQ.offer(curr.left); |
| 101 | + indexQ.offer(index - 1); |
| 102 | + } |
| 103 | + if (curr.right != null) { |
| 104 | + bfsQ.offer(curr.right); |
| 105 | + indexQ.offer(index + 1); |
| 106 | + } |
99 | 107 | }
|
100 | 108 | }
|
| 109 | + for (int i : map.keySet()) { |
| 110 | + result.add(map.get(i)); |
| 111 | + } |
| 112 | + return result; |
101 | 113 | }
|
102 |
| - for (int i : map.keySet()) { |
103 |
| - result.add(map.get(i)); |
104 |
| - } |
105 |
| - return result; |
106 | 114 | }
|
107 | 115 |
|
108 |
| - public List<List<Integer>> verticalOrder_using_hashmap(TreeNode root) { |
109 |
| - List<List<Integer>> result = new ArrayList(); |
110 |
| - if (root == null) { |
111 |
| - return result; |
112 |
| - } |
113 |
| - Queue<TreeNode> bfsQ = new LinkedList(); |
114 |
| - Queue<Integer> indexQ = new LinkedList(); |
115 |
| - HashMap<Integer, List<Integer>> map = new HashMap(); |
116 |
| - bfsQ.offer(root); |
117 |
| - indexQ.offer(0);//we set the root as index 0, left will be negative, right will be positive |
118 |
| - int min = 0; |
119 |
| - int max = 0; |
120 |
| - while (!bfsQ.isEmpty()) { |
121 |
| - int qSize = bfsQ.size(); |
122 |
| - for (int i = 0; i < qSize; i++) { |
123 |
| - TreeNode curr = bfsQ.poll(); |
124 |
| - int index = indexQ.poll(); |
125 |
| - if (map.containsKey(index)) { |
126 |
| - map.get(index).add(curr.val); |
127 |
| - } else if (!map.containsKey(index)) { |
128 |
| - List<Integer> list = new ArrayList(); |
129 |
| - list.add(curr.val); |
130 |
| - map.put(index, list); |
131 |
| - } |
132 |
| - if (curr.left != null) { |
133 |
| - bfsQ.offer(curr.left); |
134 |
| - indexQ.offer(index - 1); |
135 |
| - min = Math.min(min, index - 1); |
136 |
| - } |
137 |
| - if (curr.right != null) { |
138 |
| - bfsQ.offer(curr.right); |
139 |
| - indexQ.offer(index + 1); |
140 |
| - max = Math.max(max, index + 1); |
| 116 | + public static class Solution2 { |
| 117 | + public List<List<Integer>> verticalOrder(TreeNode root) { |
| 118 | + List<List<Integer>> result = new ArrayList(); |
| 119 | + if (root == null) { |
| 120 | + return result; |
| 121 | + } |
| 122 | + Queue<TreeNode> bfsQ = new LinkedList(); |
| 123 | + Queue<Integer> indexQ = new LinkedList(); |
| 124 | + HashMap<Integer, List<Integer>> map = new HashMap(); |
| 125 | + bfsQ.offer(root); |
| 126 | + indexQ.offer( |
| 127 | + 0);//we set the root as index 0, left will be negative, right will be positive |
| 128 | + int min = 0; |
| 129 | + int max = 0; |
| 130 | + while (!bfsQ.isEmpty()) { |
| 131 | + int qSize = bfsQ.size(); |
| 132 | + for (int i = 0; i < qSize; i++) { |
| 133 | + TreeNode curr = bfsQ.poll(); |
| 134 | + int index = indexQ.poll(); |
| 135 | + if (map.containsKey(index)) { |
| 136 | + map.get(index).add(curr.val); |
| 137 | + } else if (!map.containsKey(index)) { |
| 138 | + List<Integer> list = new ArrayList(); |
| 139 | + list.add(curr.val); |
| 140 | + map.put(index, list); |
| 141 | + } |
| 142 | + if (curr.left != null) { |
| 143 | + bfsQ.offer(curr.left); |
| 144 | + indexQ.offer(index - 1); |
| 145 | + min = Math.min(min, index - 1); |
| 146 | + } |
| 147 | + if (curr.right != null) { |
| 148 | + bfsQ.offer(curr.right); |
| 149 | + indexQ.offer(index + 1); |
| 150 | + max = Math.max(max, index + 1); |
| 151 | + } |
141 | 152 | }
|
142 | 153 | }
|
| 154 | + for (int i = min; i <= max; i++) { |
| 155 | + result.add(map.get(i)); |
| 156 | + } |
| 157 | + return result; |
143 | 158 | }
|
144 |
| - for (int i = min; i <= max; i++) { |
145 |
| - result.add(map.get(i)); |
146 |
| - } |
147 |
| - return result; |
148 | 159 | }
|
149 | 160 |
|
150 | 161 | }
|
0 commit comments