henry.ping
BAN USERTreeNode* g_pLargestBSTRoot = NULL;
int g_nSize = 0;
bool FindBST(TreeNode* pNode, int& nSize)
{
if (!pNode)
{
nSize = 0;
return FALSE;
}
// both left and right child are NULL
if (IsLeaf(pNode))
{
nSize = 1;
if (nSize > g_nSize)
{
g_nSize = nSize;
g_pLargetBSTRoot = pNode;
}
return TRUE;
}
bool bBSTLeft = pNode->left ? FALSE : TRUE;
bool bBSTRight = pNode->right ? FALSE : TRUE;
if (pNode->left)
{
bBSTLeft = FindBST(pNode->left, nSize);
}
if (pNode->right)
{
bBSTRight = FindBST(pNode->right, nSize);
}
if (bBSTLeft && bBSTRight)
{
if ((!pNode->left || pNode->left->data <= pNode->data) &&
(!pNode->right || pNode->right>data >= pNode->data))
{
nSize++;
if (nSize > g_nSize)
{
g_nSize = nSize;
g_pLargetBSTRoot = pNode;
}
return TRUE;
}
}
return FALSE;
}
- henry.ping March 06, 2011