Https://ide.codingblocks.com/s/202532

I m getting wrong in first test case.Can u please tell me where I am getting wrong.Also please tell me I have used static int i=0 int the create function.If i don’t make it static then answer comes wrong.Can u please tell me why i have been made static here.

Hello @Hk199977,

i is used to mark the index of the array preorder[] which is going to be the node of the tree.

Problem:
If you won’t make it static, then each time you’ll call the function.
It will create a node of same vale i.e. the value store at preorder[0]
Reason:
because value of i for each call will be 0.

Solution:
Make i static.
Reason:
if you’ll make i static, then the changes made in the value of i will be seen in the other function calls. i.e. it’s value won’t be 0 every time a new function call is made.
static variables have a single copy for all function call.
Thus, the increment in the value of i will remain.

Hope, this would help.
Give a like if you are satisfied.

Hello @Hk199977,

Your code is failing for test cases like:
9
50 30 5 20 60 45 70 65 80
5 30 20 50 45 60 65 70 80
Expected Output:
5
Your Output:
9

Visualization of Tree:
________________________50
______ 30 _________________________60
___5 __________20 __________45 _____________70
___________________________________65 __________80

Hope, this would help.

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.