Salem,
Sorry i am a slow learner and very confused at the moment.
Hope u have the patience.
Regarding the code u recommended, i believe it look like this:
Code:
void BST::shortestPath(node *rt,stack &s,int minHt)
{
//--------What u suggested-------------//
if ( rt == NULL )
{
if ( minHt==0 )
{ s.print();}
return;
}
//------------------------------------------//
stack.push(rt); // store node during pre-traversal
shortestPath(rt->left,s, minHt-1);
shortestPath(rt->right,s, minHt-1);
stack.pop(); // remove node during post-treversal
}
}
I am not too sure how it works. "rt" will only be NULL when minHt = -1 . This way will never print the path out, right? I'm thinking perhaps we use this instead:
Code:
void BST::shortestPath(node *rt,stack &s,int minHt)
{
// the transversing may reach an empty node before
// reaching minHt==0
if(rt==NULL)
{return;}
stack.push(rt); // store node during pre-traversal
// check for leaf and minHt reached
if(minHt == 0 && rt->left ==NULL && rt->right == NULL )
{ s.print();
return;}
shortestPath(rt->left,s, minHt-1);
shortestPath(rt->right,s, minHt-1);
stack.pop(); // remove node during post-treversal
}
}