Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented Jun 13, 2021

Resolves: #63

Algorithm:

In this problem, we have to return a lists of lists, where this list is sorted in terms of the positions of the nodes from left to right. The lists within this lists are what matters to the vertical order traversal.
So, one can think of the root being in the 0th column. The left child of root is then one unit to the left, the right child one unit to the right. This definition holds for all nodes in the tree. Every left node of a root is on a column 1 unit smaller and every right node is respectively on column at the 1 unit larger position.

Approach 1-BFS & TreeMap

We implement the aformentioned method for the determining the "column values" of the nodes. If we traverse the tree layer by layer, we will add to each column list only in a vertical sorted order. The key of this TreeMap is then the column positions, while its values are just the vertically ordered lists.
For this, we also use an auxiliary class denoting the tree node and its column position.
At then end, returning a list of lists created from the TreeMap's values is sufficient.

Approach 2-BFS & Range Preprocessing

In every tree, there is some maximum right position and some minimum left position. In all the locations therebetween, we are also guaranteed to find nodes since we are always going only 1 unit left or right.

While doing BFS, in order to avoid using a HashMap, we can shift our minimum value to an index 0 by initially denoting our root to have the column value -1*minimum.
Similar to the solution above, we place the root encapsulated in a class in a queue and place our node values in the list according to their column value. Row value is once again ignored because it is already taken care of by the level traversal of the BFS algorithm.
At the end, we directly return our list.

Approach 3-DFS & Range Preprocessing & Sorting

If we apply DFS to a tree, we won't be able to freely add the node value to the corresponding list directly. So, instead of adding just the value at some column, we add a pair of row position and the node's value itself.

Here, similarly to the previous, we have also counted the minimum and maximum column values. We iterate over the range of these values, and retrieve the column pair list from the HashMap. We sort the column list according to the row values. We then create a new list add the node values from this sorted list to here. We then add this list to the output list. We eventually return the output list.

@ErdemT09 ErdemT09 marked this pull request as ready for review June 13, 2021 09:01
@ErdemT09
Copy link
Collaborator Author

ErdemT09 commented Jun 13, 2021

Appraoch 1:
image

Approach 2:
image

Approach 3:
image


import algorithms.datastructures.TreeNode;

public class BinaryTreeVerticalOrderTraversalBFSTreeNode {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You mean BinaryTreeVerticalOrderTraversalBFSTreeMap I think.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes.

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.

@@ -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.

ErdemT09 and others added 3 commits June 13, 2021 16:06
Co-authored-by: altayhunoglu <60903744+altayhunoglu@users.noreply.github.com>
Co-authored-by: altayhunoglu <60903744+altayhunoglu@users.noreply.github.com>
Co-authored-by: altayhunoglu <60903744+altayhunoglu@users.noreply.github.com>
Copy link
Collaborator

@altay9 altay9 left a comment

Choose a reason for hiding this comment

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

Thank you, Erdem.
Nice explanation and clean code.

@ErdemT09 ErdemT09 merged commit d2b8d3d into master Jun 13, 2021
@ErdemT09 ErdemT09 deleted the 314.-Binary-Tree-Vertical-Order-Traversal branch June 13, 2021 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

314. Binary Tree Vertical Order Traversal

3 participants