-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerticalOrderTravelBinaryTree.java
141 lines (112 loc) · 3.13 KB
/
VerticalOrderTravelBinaryTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Problem Description
Given a binary tree A consisting of N nodes, return a 2-D array denoting the vertical order traversal of A.
Go through the example and image for more details.
NOTE:
If 2 or more Tree Nodes shares the same vertical level then the one with earlier occurence in the level-order(pre-order in original question)
traversal of tree comes first in the output.
Row 1 of the output array will be the nodes on leftmost vertical line similarly last row of the output array will be the nodes on the rightmost vertical line.
Problem Constraints
0 <= N <= 104
Input Format
First and only argument is an pointer to root of the binary tree A.
Output Format
Return a 2D array denoting the vertical order traversal of A.
Example Input
Input 1:
6
/ \
3 7
/ \ \
2 5 9
Input 2:
1
/ \
2 3
/ \
4 5
Example Output
Output 1:
[
[2],
[3],
[6, 5],
[7],
[9]
]
Output 2:
[
[4],
[2],
[1, 5],
[3]
]
Example Explanation
Explanation 1:
Nodes on Vertical Line 1: 2
Nodes on Vertical Line 2: 3
Nodes on Vertical Line 3: 6, 5
As 6 and 5 are on the same vertical level but as 6 comes first in the pre-order traversal of the tree so we will output 6 before 5.
Nodes on Vertical Line 4: 7
Nodes on Vertical Line 5: 9
Explanation 2:
Nodes on Vertical Line 1: 4
Nodes on Vertical Line 2: 2
Nodes on Vertical Line 3: 1, 5
Nodes on Vertical Line 4: 3
*/
/**
* Definition for binary tree
* class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) {
* val = x;
* left=null;
* right=null;
* }
* }
*/
class Node{
TreeNode tnode;
int level;
public Node(TreeNode tnode,int level){
this.tnode = tnode;
this.level = level;
}
}
public class Solution {
public ArrayList<ArrayList<Integer>> verticalOrderTraversal(TreeNode A) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(A == null) return result;
Queue<Node> queue = new LinkedList<>();
queue.add(new Node(A,0));
Map<Integer,ArrayList<Integer>> map = new TreeMap<>();
while(queue.size() > 0){
Node node = queue.poll();
TreeNode tnode = node.tnode;
if(map.containsKey(node.level)){
ArrayList<Integer> list = map.get(node.level);
list.add(tnode.val);
map.put(node.level,list);
}
else{
ArrayList<Integer> list = new ArrayList<>();
list.add(tnode.val);
map.put(node.level,list);
}
if(tnode.left != null){
queue.add(new Node(tnode.left,node.level - 1));
}
if(tnode.right != null){
queue.add(new Node(tnode.right,node.level + 1));
}
}
Iterator<Integer> itr = map.keySet().iterator();
while(itr.hasNext()){
result.add(map.get(itr.next()));
}
return result;
}
}