Cannot pass one testcase in left and right view of binary tree

Know condition for both but code cannot pass one testcase for both left and right view. The attached code link is for right view. There must be a common error in both.
code link
(https://ide.codingblocks.com/s/108350)

@Kartik
You are assuming that you will get a complete binary tree as input i.e. a tree where every level is filled completely except the last level which may be fully or partially filled. You are being level order input of a tree , the tree may be a complete binary tree , it may not be. It can even be a skew tree. Your input functions simply fails to create the appropriate tree if it is not a complete binary tree.
Refer to this code for level order input function and make the according changes in your code - https://ide.codingblocks.com/s/108450

1 Like

Thanks, for finding the mistake, I still could not find what was exactly wrong in my code but i feel that I could have used BFS approach directly which u did in your code. Thanks!

@Kartik
Your approach fails in some cases while the BFS-like approach succeeds in all cases of level order input .
Use this in your code and try again.
If your issue is over then I request you to mark your doubt as Resolved in your Doubt Section.

2 Likes

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.