Thanks for looking into the code. But the input that you give is invalid according to constraints as it is given in contraints for any child r is less than equal to number of leafs. So in this case L =2
So only possible values for childs is 1 and 2 not 5 and 7.
Oh…lol xD. I didnt use that constraint anywhere in my solution, so kind of missed that.
@ruddradev … I Was not able to implement it in contest but now if you are still stuck you can see my solution. It’s implemented exactly as stated in editorial.
Just look at
possible variable in your code. And check this test case-
Thanks @vijju123, It was a silly mistake, I didn’t reset the boolean flag for every test case which i used to check if the solution is possible or not.
Your approach is Ok, but the time complexity is wrong. It should be O(N) as there is no restriction that the binary tree is balanced. It can be skewed as well. For another easy approach, you can refer to my solution. The idea is that the number which doesn’t occur in the input is the root of the tree (The proof is easy and left as an exercise).
Can you explain what part is not clear?
@tamiliit : Thank you so much for pointing me in the right direction. I was swapping the wrong values, I had to swap the two intervals, so the start and end would become lr and rl instead of rr and ll. It was really silly of me but since you went through my code and suggested to me exactly what was needed, I got it right. Thanks again. good night.
@rj25, For correct implementation of top-down approach, an initial traversal of the tree to calculate minimum and maximum in subtree is required which is essentially a bottom-up approach. A top-down approach can then be applied later as swaps are independent at each level.
@atharva_sarage, you code is wrong here
(tree[vec[maxheight][i]].root!=1 && tree[vec[maxheight][i+1]].root!=1)
This will be an issue when one side is leaf and other side is not.
could not understand how to implement from editorial but code was simple. Was a good editorial combined with implementation code provided.