My written code is showing time exceed error and can’t think of another optimum solution. Help in finding one.
Cannot think of another optimum solution
@2304ambikagupta_35a5bee93bbf353e You cannot use ‘while’ loop instead of ‘if’ condition in preorderPrint Function bcoz that is creating an infinite loop and your code violates the time limit. except for this your code and logic are totally fine and it will not give any TLE and will pass all test cases. So instead your preorderPrint should look like this :
public void preorderPrint(Node r){
if(r == null)
return;
System.out.print(r.data+" ");
preorderPrint(r.left);
preorderPrint(r.right);
}
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.