Print All Nodes At Distance K

Only 1 test case not passed. Can you give a look at my code please!

@Kinjal, Some error is your code :-

  1. you have to print in sorted order
  2. you don’t have to explicitly find target node as if v is 0 will mean that node does’nt exist
  3. in your function you should call for right subtree only when target node is not found in left subtree
    refer code for better understanding : -
    corrected code :- https://ide.codingblocks.com/s/249513

In case of any doubt feel free to ask :slight_smile:
Mark your doubt as RESOLVED if you got the answer

So all test cases passed. Thank you for your refer code.
Now I need to know that when you talk about point no. 3, you mean that if I dont do that it’ll just take more time. Right!

yeah, that will take more time as if we know that target node is in left subtree than what;s the point in finding inn right subtree

Yes, I agree that. How’s my style of code, do you think that I can do it better? I mean, is my style of code too long or too lame as newbie? Any advice!

programming practices are evolved through time and practice, just avoid any unnecessary computation and always go for optimization

1 Like

and space too. hahahaha.