-
Notifications
You must be signed in to change notification settings - Fork 7
314. Binary Tree Vertical Order Traversal #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f01403b
314-Add Initial Solution
ErdemT09 29dc56d
314-Refactor BFS Solution
ErdemT09 a861aff
314-Add Improved BFS Solution
ErdemT09 a318f05
314-Add Initial DFS Solution
ErdemT09 1da5c3b
314-Refactor DFS Solution
ErdemT09 e9291e5
314-Update All Solutions
ErdemT09 588326b
314-Utilize Tree Range
ErdemT09 70fa33b
314-Fix Class Name
ErdemT09 ef8d1a1
314-Change Variable Access Modifier
ErdemT09 fdd0cf3
314-Remove Variable 1
ErdemT09 8a43d69
314-Remove Variable 2
ErdemT09 94e0174
314-Remove Variable 3
ErdemT09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
...rated170/medium/binarytreeverticalordertraversal/BinaryTreeVerticalOrderTraversalBFS.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| 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)); | ||
| } | ||
| } | ||
71 changes: 71 additions & 0 deletions
71
...0/medium/binarytreeverticalordertraversal/BinaryTreeVerticalOrderTraversalBFSTreeMap.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
...rated170/medium/binarytreeverticalordertraversal/BinaryTreeVerticalOrderTraversalDFS.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package algorithms.datastructures; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.