here is my code:-
All test cases are pass except one. Can you tell the corner case I am not getting.
here is my code:-
All test cases are pass except one. Can you tell the corner case I am not getting.
It should be correct but I don’t know why one test case failed.
@Jitinkansal
Your approach is not completely correct
your are not considering the all test case
look at second example tree discuss below
this is the case in which you get wrong ans
i have discussed the detail solution of the problem read this
this will be helpful to understand your mistake
Given a Binary Tree, we need to 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.
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 bottom-most 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.
You are getting 5 10 4 22 25
Approach
The following are steps to print Bottom View of Binary Tree.
you can take help from reference Code
Reference Code
if you have more doubts regarding this feel free to ask
i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved 