Tree left view(2 testcases getting wrong )

There are two mistakes in your code

  1. You have taken an array of size 10^7 in a local scope which might take a huge amount of time and might give in appropriate results

  2. You are only taking left most elements in your left view. Let’s consider total levels in left side is only2 and other nodes in right side have level 3 then you have to print the nodes of the right most side as well. Which can be viewed from left.

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.

I made some changes to the code


Still getting runerror in testcase 2 and wrong answer in testcase 3

I would recommend you to remove the function which you don’t need. See debugging 300 lines of code can be really difficult. And there’s a possibility that you have some error in that part of your code which you don’t use. So first change that.

Also in line 239 inside left view function you have initiated an array of size 10^7 which is not correct. Create a global array to avoid runtime errors.

Also your implementation of left view function is wrong after printing the left part of the tree.
After printing left you are always going right which might not be correct.
So try correcting that.

my code is giving now timelimit exceeded in two testcases

You are not updating the temp1 pointer correctly inside the FindleftView.
Also the you are declaring a very large array that would also result in error.
The way you have written program it will not print the leftview (if you actually want to find that only)
For finding the leftview, you need to use levelorder traversal and print the first node at each level.

Now code is short but still giving TLE

thanks for helping me!!!