Sunday, April 22, 2012
binary tree
find the nth largest number in a binary search tree
input: bst tree root, holder of result
output: error code
int bstNthLargest(node *root, int *result, int *n)
{
if(!root) return -1;
if(root->right) bstNthLargest(root->right, result, n);
if(*n==0) return 0;
if(*n==1)
{
*result = root->v;
*n = 0;
return 0;
}
*n--;
if(root->left) bstNthLargest(root->left, result, n);
return -1;
}
find binary tree height
int getTreeHeight(node *root)
{
if(!root) return 0;
if(!root->left && !root->right) return 1;
int leftH = getTreeHight(root->left);
int rightH = getTreeHight(root->right);
return max(leftH, rightH)+1;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment