99import java .util .Queue ;
1010import java .util .TreeMap ;
1111
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).
16-
17- If two nodes are in the same row and column, the order should be from left to right.
18-
19- Examples:
20-
21- Given binary tree [3,9,20,null,null,15,7],
22- 3
23- /\
24- / \
25- 9 20
26- /\
27- / \
28- 15 7
29- return its vertical order traversal as:
30- [
31- [9],
32- [3,15],
33- [20],
34- [7]
35- ]
36-
37- Given binary tree [3,9,8,4,0,1,7],
38- 3
39- /\
40- / \
41- 9 8
42- /\ /\
43- / \/ \
44- 4 01 7
45- return its vertical order traversal as:
46- [
47- [4],
48- [9],
49- [3,0,1],
50- [8],
51- [7]
52- ]
53-
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),
55- 3
56- /\
57- / \
58- 9 8
59- /\ /\
60- / \/ \
61- 4 01 7
62- /\
63- / \
64- 5 2
65- return its vertical order traversal as:
66- [
67- [4],
68- [9,5],
69- [3,0,1],
70- [8,2],
71- [7]
72- ]
73- */
7412public class _314 {
7513 public static class Solution1 {
7614 public List <List <Integer >> verticalOrder (TreeNode root ) {
@@ -83,7 +21,7 @@ public List<List<Integer>> verticalOrder(TreeNode root) {
8321 TreeMap <Integer , List <Integer >> map = new TreeMap ();
8422 bfsQ .offer (root );
8523 indexQ .offer (
86- 0 );//we set the root as index 0, left will be negative, right will be positive
24+ 0 );//we set the root as index 0, left will be negative, right will be positive
8725 while (!bfsQ .isEmpty ()) {
8826 int qSize = bfsQ .size ();
8927 for (int i = 0 ; i < qSize ; i ++) {
@@ -124,7 +62,7 @@ public List<List<Integer>> verticalOrder(TreeNode root) {
12462 HashMap <Integer , List <Integer >> map = new HashMap ();
12563 bfsQ .offer (root );
12664 indexQ .offer (
127- 0 );//we set the root as index 0, left will be negative, right will be positive
65+ 0 );//we set the root as index 0, left will be negative, right will be positive
12866 int min = 0 ;
12967 int max = 0 ;
13068 while (!bfsQ .isEmpty ()) {
0 commit comments