Why am I getting -1 as output? Can you tell whats wrong

hello @akash_281

when u are htting if(root==target) then from here u need to return 0. which u r not doing

Fixed it but it’s still showing -1

becuase u have create a new node for 2. then its obvious that it will have different address from the tree nnode hence it is giving -1.

u need to find the address of tree nodewith data 2.

or instead of comparing address compre data value

Understood but how to take the input then? If my target node has a value of 2
if I do something like root->left I am getting the correct ans but is there any other way because if my tree is huge this is not sustainable.

compare data instead of address.