*劍指offer-----二叉樹的下一個節點
阿新 • • 發佈:2018-12-13
題目:
思路:
程式碼:
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; }