Monday, March 4, 2013

find the kth largest node of a BSTBST


void kth_largest(Node* root,unsigned int k)
{
if(!root)return;
stack<Node*> myStack;
Node* curNode=root;
int i=0;
while(curNode || !myStack.empty())
{
if(curNode)
{
myStack.push(curNode);
curNode=curNode->L;
}
else
{
Node* topNode=myStack.top();
curNode=topNode->R;
myStack.pop();
i++;
if(i==k)
cout<<topNode->val<<endl;
}
}
}

No comments:

Post a Comment