Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package algorithms.curated170.medium.braceexpansion;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.TreeMap;

import algorithms.datastructures.TreeNode;

public class BinaryTreeVerticalOrderTraversalBFS {

public List<List<Integer>> verticalOrder(TreeNode root) {
if (root == null) {
return Collections.emptyList();
}


int[] range = new int[] {0, 0};
getRange(root, range, 0);

List<List<Integer>> columnsAtXPos = new ArrayList<>(range[1]-range[0] + 2);

for (int i = range[0]; i <= range[1]; i++) {
columnsAtXPos.add(new ArrayList<Integer>());
}

Deque<NodePosPair> q = new ArrayDeque<>();
q.offer(new NodePosPair(root, -1*range[0]));

while (!q.isEmpty()) {
NodePosPair npp = q.poll();
npp.placeIntoMap(columnsAtXPos);
npp.queueChildren(q);
}


return columnsAtXPos;
}

public void getRange(TreeNode root, int[] range, int col) {
if (root == null) {
return;
}


range[0] = Math.min(range[0], col);
range[1] = Math.max(range[1], col);

getRange(root.left, range, col - 1);
getRange(root.right, range, col + 1);
}

private class NodePosPair {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can define NodePosPair in an external class file to use it in multiple classes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the two BFS solutions, the implementation is not the same. So, I think it's not worth bothering as we won't use it again and it would create problems while testing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, of course. No problem.

private final TreeNode node;
private final int x;

void placeIntoMap(List<List<Integer>> columnTable) {
columnTable.get(x).add(node.val);
}

void queueChildren(Deque<NodePosPair> q) {
if (node.left != null) {
q.offer(new NodePosPair(node.left, x - 1));
}
if (node.right != null) {
q.offer(new NodePosPair(node.right, x + 1));
}
}

NodePosPair(TreeNode tn, int x) {
this.node = tn;
this.x = x;
}
}

public static void main(String[] args) {
var solution = new BinaryTreeVerticalOrderTraversalBFS();

TreeNode tn1 = new TreeNode(1);
TreeNode t0 = new TreeNode(2, null, tn1);
TreeNode t1 = new TreeNode(5);
TreeNode t2 = new TreeNode(4, null, t1);
TreeNode t3 = new TreeNode(3, t0, t2);
TreeNode root = new TreeNode(9, null, t3);

System.out.println(solution.verticalOrder(root));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package algorithms.curated170.medium.braceexpansion;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.TreeMap;

import algorithms.datastructures.TreeNode;

public class BinaryTreeVerticalOrderTraversalBFSTreeMap {
public List<List<Integer>> verticalOrder(TreeNode root) {

if (root == null) {
return Collections.emptyList();
}

TreeMap<Integer, ArrayList<Integer>> columnTable = new TreeMap<>();

Deque<NodePosPair> q = new ArrayDeque<>();
q.offer(new NodePosPair(root, 0));

while (!q.isEmpty()) {
NodePosPair npp = q.poll();
npp.placeIntoMap(columnTable);
npp.queueChildren(q);
}

List<List<Integer>> columnsAtXPos = new ArrayList<>(columnTable.values());

return columnsAtXPos;
}

private class NodePosPair {
private final TreeNode node;
private final int x;

void placeIntoMap(TreeMap<Integer, ArrayList<Integer>> columnTable) {
columnTable.putIfAbsent(x, new ArrayList<>());
columnTable.get(x).add(node.val);
}

void queueChildren(Deque<NodePosPair> q) {
if (node.left != null) {
q.offer(new NodePosPair(node.left, x - 1));
}
if (node.right != null) {
q.offer(new NodePosPair(node.right, x + 1));
}
}

NodePosPair(TreeNode tn, int x) {
this.node = tn;
this.x = x;
}
}

public static void main(String[] args) {
var solution = new BinaryTreeVerticalOrderTraversalBFSTreeMap();

TreeNode tn1 = new TreeNode(1);
TreeNode t0 = new TreeNode(2, null, tn1);
TreeNode t1 = new TreeNode(5);
TreeNode t2 = new TreeNode(4, null, t1);
TreeNode t3 = new TreeNode(3, t0, t2);
TreeNode root = new TreeNode(9, null, t3);

System.out.println(solution.verticalOrder(root));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package algorithms.curated170.medium.braceexpansion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import algorithms.datastructures.TreeNode;

public class BinaryTreeVerticalOrderTraversalDFS {

HashMap<Integer, ArrayList<int[]>> columnTable = new HashMap<>();
int minColumn = 0, maxColumn = 0;

public List<List<Integer>> verticalOrder(TreeNode root) {

if (root == null) {
return Collections.emptyList();
}

DFS(root, 0, 0);

List<List<Integer>> columnsAtXPos = new ArrayList<>(maxColumn - minColumn + 2);

for (int i = minColumn; i <= maxColumn; i++) {

Collections.sort(columnTable.get(i), (p1, p2) -> p1[0] - p2[0]);

List<Integer> sortedColumn = new ArrayList<>();

for (int[] rowValPair : columnTable.get(i)) {
sortedColumn.add(rowValPair[1]);
}

columnsAtXPos.add(sortedColumn);
}

return columnsAtXPos;
}

private void DFS(TreeNode root, int row, int col) {

if (root == null) {
return;
}

columnTable.putIfAbsent(col, new ArrayList<>());

columnTable.get(col).add(new int[] { row, root.val });
minColumn = Math.min(minColumn, col);
maxColumn = Math.max(maxColumn, col);

DFS(root.left, row + 1, col - 1);
DFS(root.right, row + 1, col + 1);
}

public static void main(String[] args) {
var solution = new BinaryTreeVerticalOrderTraversalDFS();

TreeNode tn1 = new TreeNode(1);
TreeNode t0 = new TreeNode(2, null, tn1);
TreeNode t1 = new TreeNode(5);
TreeNode t2 = new TreeNode(4, null, t1);
TreeNode t3 = new TreeNode(3, t0, t2);
TreeNode root = new TreeNode(9, null, t3);

System.out.println(solution.verticalOrder(root));
}

}
15 changes: 15 additions & 0 deletions src/main/java/algorithms/datastructures/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package algorithms.datastructures;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have also added this. Might be useful at some point.


public class Pair<T1, T2> {
public T1 first;
public T2 second;

public Pair() {

}

public Pair(T1 first, T2 second) {
this.first = first;
this.second = second;
}
}