https://github.com/emahtab/binary-tree-vertical-order-traversal
Binary Tree Vertical Order Traversal
https://github.com/emahtab/binary-tree-vertical-order-traversal
bfs binary-tree-traversal leetcode problem-solving vertical-order-traversal
Last synced: 3 months ago
JSON representation
Binary Tree Vertical Order Traversal
- Host: GitHub
- URL: https://github.com/emahtab/binary-tree-vertical-order-traversal
- Owner: eMahtab
- Created: 2020-05-12T17:11:14.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-05-01T07:36:35.000Z (about 3 years ago)
- Last Synced: 2025-02-02T03:25:47.016Z (5 months ago)
- Topics: bfs, binary-tree-traversal, leetcode, problem-solving, vertical-order-traversal
- Homepage:
- Size: 49.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Binary Tree Vertical Order Traversal
## https://leetcode.com/problems/binary-tree-vertical-order-traversal
## https://www.lintcode.com/problem/651Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column).
#### Very Important 😯: If two nodes are in the same row and column, the order should be from left to right.

```
Examples 1:Input: [3,9,20,null,null,15,7]
3
/\
/ \
9 20
/\
/ \
15 7Output:
[
[9],
[3,15],
[20],
[7]
]
Examples 2:Input: [3,9,8,4,0,1,7]
3
/\
/ \
9 8
/\ /\
/ \/ \
4 01 7Output:
[
[4],
[9],
[3,0,1],
[8],
[7]
]
Examples 3:Input: [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)
3
/\
/ \
9 8
/\ /\
/ \/ \
4 01 7
/\
/ \
5 2Output:
[
[4],
[9,5],
[3,0,1],
[8,2],
[7]
]
```# Implementation :
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
class Node {
TreeNode treeNode;
int horizontalDistance;
Node(TreeNode treeNode, int horizontalDistance) {
this.treeNode = treeNode;
this.horizontalDistance = horizontalDistance;
}
}
public List> verticalOrder(TreeNode root) {
List> res = new ArrayList<>();
if(root == null) return res;
Queue q = new LinkedList<>();
q.offer(new Node(root,0));
Map> map = new HashMap<>();
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
while(!q.isEmpty()) {
Node current = q.poll();
List list = map.getOrDefault(current.horizontalDistance, new ArrayList());
list.add(current.treeNode.val);
map.put(current.horizontalDistance,list);
min = Math.min(min, current.horizontalDistance);
max = Math.max(max, current.horizontalDistance);
if(current.treeNode.left != null)
q.offer(new Node(current.treeNode.left, current.horizontalDistance - 1));
if(current.treeNode.right != null)
q.offer(new Node(current.treeNode.right, current.horizontalDistance + 1));
}
for(int i = min; i <= max; i++) {
res.add(map.get(i));
}
return res;
}
}
```# References :
https://www.youtube.com/watch?v=QWbVCqIhTO4