*剑指offer-----二叉树的下一个节点

题目:

思路:

代码:

BinaryTreeNode* GetNext(BinaryTreeNode* pNode)
{
	if(pNode == NULL)
		return NULL;
		
	BinaryTreeNode* pNext = NULL;
	if(pNode->m_pRight != NULL)
	{
		BinaryTreeNode* pRight = pNode->m_pRight;
		while(pRight->m_pLeft != NULL)
			pRight = pRight->m_pLeft;
			
		pNext = pRight;
	}
	else if(pNode->m_pParent != NULL)
	{
		BinaryTreeNode* pCurrent = pNode;
		BinaryTreeNode* pParent = pNode->m_pParent;
		while(pParent != NULL && pCurrent == pParent->m_pRight)
		{
			pCurrent = pParent;
			pParent = pParent->m_pParent;
		}
		pNext = pParent;
	}
	
	return pNext;
}

猜你喜欢

转载自blog.csdn.net/qq_39503189/article/details/82924387