// your inorder function is not correct
bool inorder(Node* node, int* x, Node* root) {
if (root==NULL)
return false;
if (inorder(node, x, root->left))
return true;
if (node==root) //inorder property here
return true;
(*x)++;
if (inorder(node, x, root->right))
return true;
return false;
}
int x0(Node *node) {
int x=0;
if (inorder(node, &x, theRoot)) //you did't pass root here
return x;
else //not found
return -1;
}
Points:
- You didn't pass the root and checked with the query node. That's not correct. You need to search the tree from the root.
- For any inorder property, basic idea is you go left, do what you want, then go right. But this may vary according to your problem.
- If the query node is found, no further
inorder
call takes place. It returns all the way tox0
withx
containing the desired value.