|
| 1 | +package medium; |
| 2 | + |
| 3 | +import classes.TreeNode; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.LinkedList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Queue; |
| 11 | +import java.util.TreeMap; |
| 12 | + |
| 13 | +public class BinaryTreeVerticalOrderTraversal { |
| 14 | + public List<List<Integer>> verticalOrder_using_treemap(TreeNode root) { |
| 15 | + List<List<Integer>> result = new ArrayList(); |
| 16 | + if(root == null) return result; |
| 17 | + Queue<TreeNode> bfsQ = new LinkedList(); |
| 18 | + Queue<Integer> indexQ = new LinkedList(); |
| 19 | + TreeMap<Integer, List<Integer>> map = new TreeMap(); |
| 20 | + bfsQ.offer(root); |
| 21 | + indexQ.offer(0);//we set the root as index 0, left will be negative, right will be positive |
| 22 | + while(!bfsQ.isEmpty()){ |
| 23 | + int qSize = bfsQ.size(); |
| 24 | + for(int i = 0; i < qSize; i++){ |
| 25 | + TreeNode curr = bfsQ.poll(); |
| 26 | + int index = indexQ.poll(); |
| 27 | + if(map.containsKey(index)){ |
| 28 | + map.get(index).add(curr.val); |
| 29 | + } else if(!map.containsKey(index)){ |
| 30 | + List<Integer> list = new ArrayList(); |
| 31 | + list.add(curr.val); |
| 32 | + map.put(index, list); |
| 33 | + } |
| 34 | + if(curr.left != null){ |
| 35 | + bfsQ.offer(curr.left); |
| 36 | + indexQ.offer(index-1); |
| 37 | + } |
| 38 | + if(curr.right != null){ |
| 39 | + bfsQ.offer(curr.right); |
| 40 | + indexQ.offer(index+1); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + for(int i : map.keySet()){ |
| 45 | + result.add(map.get(i)); |
| 46 | + } |
| 47 | + return result; |
| 48 | + } |
| 49 | + |
| 50 | + public List<List<Integer>> verticalOrder_using_hashmap(TreeNode root) { |
| 51 | + List<List<Integer>> result = new ArrayList(); |
| 52 | + if(root == null) return result; |
| 53 | + Queue<TreeNode> bfsQ = new LinkedList(); |
| 54 | + Queue<Integer> indexQ = new LinkedList(); |
| 55 | + HashMap<Integer, List<Integer>> map = new HashMap(); |
| 56 | + bfsQ.offer(root); |
| 57 | + indexQ.offer(0);//we set the root as index 0, left will be negative, right will be positive |
| 58 | + int min = 0, max = 0; |
| 59 | + while(!bfsQ.isEmpty()){ |
| 60 | + int qSize = bfsQ.size(); |
| 61 | + for(int i = 0; i < qSize; i++){ |
| 62 | + TreeNode curr = bfsQ.poll(); |
| 63 | + int index = indexQ.poll(); |
| 64 | + if(map.containsKey(index)){ |
| 65 | + map.get(index).add(curr.val); |
| 66 | + } else if(!map.containsKey(index)){ |
| 67 | + List<Integer> list = new ArrayList(); |
| 68 | + list.add(curr.val); |
| 69 | + map.put(index, list); |
| 70 | + } |
| 71 | + if(curr.left != null){ |
| 72 | + bfsQ.offer(curr.left); |
| 73 | + indexQ.offer(index-1); |
| 74 | + min = Math.min(min, index-1); |
| 75 | + } |
| 76 | + if(curr.right != null){ |
| 77 | + bfsQ.offer(curr.right); |
| 78 | + indexQ.offer(index+1); |
| 79 | + max = Math.max(max, index+1); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + for(int i = min; i <= max; i++){ |
| 84 | + result.add(map.get(i)); |
| 85 | + } |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments