Associated Course(s):
Sudo Placement [IITs]

Given a binary tree, print the bottom view from left to right. A node x is there in output if x is the bottommost node at its horizontal distance from root. Horizontal distance of left child of a node x is equal to horizontal distance of x minus 1, and that of right child is horizontal distance of x plus 1.

Examples:

20

/ \

8 22

/ \ \

5 3 25

/ \

10 14

For the above tree the output should be 5, 10, 3, 14, 25.

If there are multiple bottom-most nodes for a horizontal distance from root, then print the later one in level traversal. For example, in the below diagram, 3 and 4 are both the bottommost nodes at horizontal distance 0, we need to print 4.

20

/ \

8 22

/ \ / \

5 3 4 25

/ \

10 14

For the above tree the output should be 5, 10, 4, 14, 25.

**Input:**

The input contains T, deonoting the number of testcases. Each testcase contains 2 lines. The first line of each testcase contains number of nodes and second line contains data nodes. The task is to complete the method which takes one argument, root of Binary Tree. The struct Node has a data part which stores the data, pointer to left child and pointer to right child. There are multiple test cases. For each test case, this method will be called individually.

**Output:**

The function should print nodes in bottom view of Binary Tree. Your code should not print a newline, it is added by the caller code that runs your function.

**Constraints:**

1 <=T<= 30

0 <= Number of nodes<= 100

0 <= Data of a node<= 1000

**Example:
Input:**

2

2

1 2 R 1 3 L

4

10 20 L 10 30 R 20 40 L 20 60 R

**Output:**

3 1 2

40 20 60 30

**Explanation:
Testcase 1:** First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 3 and right child of 1 is 2.

Thus nodes of binary tree will be printed as such 3 1 2.

Author: kartik

blackshadows | 210 |

xmyqsh | 198 |

hanuman001 | 164 |

Adarsh Trivedi | 148 |

layman_brother | 144 |

blackshadows | 692 |

xmyqsh | 539 |

aman19 | 398 |

r0c2048 | 360 |

kspk | 331 |

akhayrutdinov | 5005 |

Ibrahim Nash | 4871 |

Quandray | 4338 |

sanjay05 | 3668 |

blackshadows | 3196 |

Login to report an issue on this page.