9
9
import java .util .Queue ;
10
10
import java .util .TreeMap ;
11
11
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
- */
74
12
public class _314 {
75
13
public static class Solution1 {
76
14
public List <List <Integer >> verticalOrder (TreeNode root ) {
@@ -83,7 +21,7 @@ public List<List<Integer>> verticalOrder(TreeNode root) {
83
21
TreeMap <Integer , List <Integer >> map = new TreeMap ();
84
22
bfsQ .offer (root );
85
23
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
87
25
while (!bfsQ .isEmpty ()) {
88
26
int qSize = bfsQ .size ();
89
27
for (int i = 0 ; i < qSize ; i ++) {
@@ -124,7 +62,7 @@ public List<List<Integer>> verticalOrder(TreeNode root) {
124
62
HashMap <Integer , List <Integer >> map = new HashMap ();
125
63
bfsQ .offer (root );
126
64
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
128
66
int min = 0 ;
129
67
int max = 0 ;
130
68
while (!bfsQ .isEmpty ()) {
0 commit comments