Following is a recursive function. Use ids of p and q to identify the p and
q node. It is not very elegant to me. Any suggestions to improve it.
Thanks
void countPQ(TreeNode *root, int idP, bool &startP, int& counterP, int idQ,
bool &startQ, int &counterQ)
{
if(root==NULL)
{
return;
}
countPQ(root->left, idP, startP, counterP, idQ, startQ, counterQ);
if( root->val==idP ) startP=false;
if(startP) counterP++;
if( root->val==idQ ) startQ=false;
if(startQ) counterQ++;
countPQ(root->right, idP, startP, counterP, idQ, startQ, counterQ);
}
int main()
{
int counterP=0;;
bool startP=true;
int counterQ=0;
bool startQ=true;
countPQ(root, 25, startP, counterP, 15, startQ, counterQ);
cout<counterQ-counterP-1<return 0;
}