题目:
求二叉树中序遍历的下一个结点。
根据二叉树中序遍历的特点,中序遍历是从小到大排序的,以左根右顺序依次遍历二叉树。
/找出中序遍历的下一个结点
struct BinaryTree
{
int data;
BinaryTree *left;
BinaryTree *right;
BinaryTree *parent;
};
BinaryTree *GetNext(BinaryTree *pNode)
{
if (pNode == NULL)
return NULL;
BinaryTree *pNext = NULL;
if (pNode->right != NULL)
{
BinaryTree *pRight = pNode->right;
while (pRight->left != NULL)
pRight = pRight->left;
pNext = pRight;
}
else if (pNode->parent != NULL)
{
BinaryTree *temp = pNode;
BinaryTree *pParent = pNode->parent;
while (pNode->parent != NULL&&pNode == pNode->parent->right)
{
temp = pParent;
pParent = pParent->parent;
}
pNext = pParent;
}
return pNext;
}