题目:

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

思路:

*剑指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;
}

 

相关文章:

  • 2021-10-03
  • 2021-04-26
  • 2021-07-15
  • 2021-03-28
  • 2021-06-13
  • 2022-01-03
  • 2021-05-23
猜你喜欢
  • 2021-08-01
  • 2021-06-26
  • 2021-04-22
  • 2021-07-17
  • 2021-07-21
  • 2021-06-21
相关资源
相似解决方案