From the above image, we can see that, Root node is at scale 0. With the reference from Root node which is at distance(column) 0, we will calculate the horizontal distance of each node at left(Negative) side and right(Positive) side.

Lets understand it with example:1. Node 1 is at distance 0 as this is the start point in scale. 2. Node 2 is at distance -1. (horizontal distance between root node 1 and node 2), as we are moving 1 column on left side of scale.3. Node 4 is at distance -2 (horizontal distance between root node 1 and node 4), as we are moving 2 column on left side of scale.4. Node 5 is at distance 0 (horizontal distance between root node 1 and node 5), as we are again on 0th column on scale. Remember we are only interested on nodes falling on same column.5. Node 3 is at distance +1 (horizontal distance between root node 1 and node 3), as we are moving 1 column on right side of scale.6. Node 6 is at distance 0 (horizontal distance between root node 1 and node 6), as we are again on 0th column on scale. 7. Node 7 is at distance +2 (horizontal distance between root node 1 and node 7), as we are moving 2 column on right side of scale.

From the above understanding, can we say,If two nodes have the same Horizontal Distance from root, then they are on same vertical line.

This is what exactly we will do, we will keep one variable horizontal distance(hd) which will keep track of horizontal distance of each node from root node,

Algorithm

1. Read the tree in Pre-order traversal2. First node encounter will be root node, which is at hd 0 as this is starting point in scale. (hd is 0)

3. From this point, When we will move on Left, that is we are moving one column on left side, we will do (hd-1), When we will move on Right, that is we are moving one column on right side, we will do (hd+1).4. We will take one map, which will keep dumping all the nodes found at same horizontal distance from root. (for map, key is hd and value will be nodes found).

For each node encountered, check whether distance is already present in map, If it is present then we already encountered nodes at same distance prior to this node, So append current node data to existing nodes.

If it is not present then this is the first time we are encountering this distance, so simply store the data against hd.

5. Better to use TreeMap in this case, as it will store the result in ascending order, which we are interested in.

In above solution, Vertical order traversal of a tree is working fine, but nodes at same level are not printed in sequence they are present because we are doing Pre-order traversal of a tree (Depth first traversal) in which Left sub-tree nodes are read first and then it picks right sub-tree.

For mainlining sequence, we have to do Breadth first traversal that is Level order traversal, which make sure nodes are Lower levels are read first than Nodes at higher level that is all Nodes at Level 0 are read first and then Node at Level 1 and then Node at Level 2 and so on.

For this we have to take a queue, which will preserve nodes at same level for later processing.So changes that need to be done in above solution is shown below,

Find Vertical Sum of given Binary Tree

Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines.Let us first understand what we want to achieve? what is the input and what will be the expected output?

Solution

Solution for finding Vertical sum of binary tree is very similar to Print Binary tree vertically as we saw above.

Solution for finding Vertical sum only differ in adding the nodes at same vertical line instead of printing them all, which we did in printing Binary tree vertically above.

For maintaining sequence, we have to read the tree in Level Order which will make sure that Nodes at Lower levels are read first than higher levels, that is all Nodes at Level 0 are read first and then Node at Level 1 and then Node at Level 2 and so on.

Updated the solution, I hope this is what you are looking for. Please let me know if there is any question.

Post a Comment

I'm Jayesh Patel, author of "JavaByPatel". I'm not a professional blogger but when time permits, love to share in-depth solutions to common Interview questions asked.Any questions/feedback, Please drop a mail atjayeshmaheshpatel@gmail.com