struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
int GetNodeNum(BinaryTreeNode * pRoot)
{
if (pRoot == NULL)
{
return 0;
}
else
{
return GetNodeNum(pRoot->m_pLeft) + GetNodeNum(pRoot->m_pRight) + 1;
}
}
int GetDepth(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
{
return 0;
}
int depthLeft = GetDepth(pRoot->m_pLeft);
int depthRight = GetDepth(pRoot->m_pRight);
return depthLeft > depthRight ? (depthLeft + 1) : (depthRight + 1);
}
void PreOrderTraverse(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
{
return;
}
print(pRoot);
PreOrderTraverse(pRoot->m_pLeft);
PreOrderTraverse(pRoot->m_pRight);
}
void LevelTraverse(BinaryTreeNode* Root)
{
if (Root == NULL)
{
return;
}
queue<BinaryTreeNode *> q;
q.push(Root);
while (!q.empty())
{
BinaryTreeNode * pNode = q.front();
q.pop();
visit(pNode);
if (pNode->m_pLeft != NULL)
{
q.push(pNode->m_pLeft);
}
if (pNode->m_pRight != NULL)
{
q.push(pNode->m_pRight);
}
}
return;
}
void Convert(BinaryTreeNode* Root, BinaryTreeNode* &pFirstNode, BinaryTreeNode* &pLastNode)
{
BinaryTreeNode *pFirstLeft, *pFistRight, *pLastRight, *pLastLeft;
if (Root == NULL)
{
pFirstNode = NULL;
pLastNode = NULL;
return;
}
if (Root->m_pLeft == NULL)
{
pFirstNode = Root;
}
else
{
Convert(Root->m_pLeft, pFirstLeft, pLastLeft);
pFirstNode = pFirstLeft;
Root->m_pLeft = pLastLeft;
pLastLeft->m_pRight = Root;
}
if (Root->m_pRight ==NULL)
{
pLastNode = Root;
}
else
{
Convert(Root->m_pRight, pFistRight, pLastRight);
pLastNode = pLastRight;
Root->m_pRight = pFistRight;
pFistRight->m_pLeft = Root;
}
return;
}
int GetNodeNumKthLevel(BinaryTreeNode* Root, int k)
{
if (Root == NULL|| k<1)
{
return 0;
}
if (k == 1)
{
return 1;
}
int numLeft = GetNodeNumKthLevel(Root->m_pLeft, k - 1);
int numRight = GetNodeNumKthLevel(Root->m_pRight, k - 1);
return numLeft + numRight;
}
int GetLeafNum(BinaryTreeNode* Root)
{
if (Root == NULL)
{
return 0;
}
if (Root->m_pLeft ==NULL&&Root->m_pRight==NULL)
{
return 1;
}
int numLeft = GetLeafNum(Root->m_pLeft);
int numRight = GetLeafNum(Root->m_pRight);
return numRight + numLeft;
}
bool StructureCmp(BinaryTreeNode* Root1, BinaryTreeNode* Root2)
{
if (Root2== NULL && Root1==NULL)
{
return true;
}
else if (Root1==NULL||Root2==NULL)
{
return false;
}
bool leftResult = StructureCmp(Root1->m_pLeft, Root2->m_pLeft);
bool rightResult = StructureCmp(Root1->m_pRight, Root2->m_pRight);
return (leftResult&rightResult);
}
bool IsAVL(BinaryTreeNode* Root,int &Hight)
{
if (Root == NULL)
{
Hight = 0;
return true;
}
int hightLeft;
bool resultLeft = IsAVL(Root->m_pLeft,hightLeft);
int hightRight;
bool resultRight = IsAVL(Root->m_pRight,hightRight):
if (resultLeft && resultRight && abs(hightRight - hightLeft) <=1)
{
Hight = max(hightLeft, hightRight) + 1;
return true;
}
else
{
Hight = max(hightLeft, hightRight) + 1;
return false;
}
}
BinaryTreeNode* Mirror(BinaryTreeNode* Root)
{
if (Root == NULL)
{
return NULL;
}
BinaryTreeNode* pLeft = Mirror(Root->m_pLeft);
BinaryTreeNode* pRight = Mirror(Root->m_pRight);
Root->m_pLeft = pRight;
Root->m_pRight = pLeft;
return Root;
}
bool FindNode(BinaryTreeNode* Root, BinaryTreeNode* pNode)
{
if (pRoot == NULLl || pNode == NULL)
{
return false;
}
if (Root == pNode)
{
return true;
}
bool found = FindNode(Root->m_pLeft, pNode);
if (!found)
{
found = FindNode(Root->m_pRight, pNode);
}
return found;
}
BinaryTreeNode* GetLastCommonParent(BinaryTreeNode* Root, BinaryTreeNode* pNode1, BinaryTreeNode* pNode2)
{
if (FindNode(Root->m_pLeft,pNode1))
{
if (FindNode(Root->m_pRight,pNode2)
{
return Root;
}
else
{
return GetLastCommonParent(Root->m_pLeft, pNode1, pNode2);
}
}
else
{
if (FindNode(Root->m_pLeft, pNode2))
{
return Root;
}
else
{
return GetLastCommonParent(Root->m_pRight, pNode1, pNode2);
}
}
}
bool GetNodePath(BinaryTreeNode* Root, BinaryTreeNode* Node, list<BinaryTreeNode*> &path)
{
if (Root == pNode)
{
path.push_back(Root);
return true;
}
if (Root == NULL)
{
return false;
}
path.push_back(Root);
bool found = GetNodePath(Root->m_pLeft, pNode, path);
if (!found)
{
found = GetNodePath(Root->m_pRight, pNode, path);
}
if (!found)
{
path.pop_back();
}
return found;
}
BinaryTreeNode* GetLastCommonParent(BinaryTreeNode* Root, BinaryTreeNode* pNode1, BinaryTreeNode* pNode2)
{
if (Root->NULL || pNode1 == NULL || pNode2 == NULL)
{
return NULL;
}
list<BinaryTreeNode*> path1;
bool bResult1 = GetNodePath(Root, pNode1, path1);
list<BinaryTreeNode*> path2;
bool bResult2 = GetNodePath(Root, pNode2, path2);
if (!bResult1 || !bResult2)
{
return NULL;
}
BinaryTreeNode* pLast = NULL;
list<BinaryTreeNode*>::const_iterator iter1 = path1.begin();
list<BinaryTreeNode*>::const_iterator iter2 = path2.begin();
while (iter1 != pahth1.end() && iter2!= path2.end())
{
if (*iter1 == *iter2)
{
pLast = *iter1;
}
else
{
break;
}
iter1++;
iter2++;
}
return pLast;
}
int GetMaxDistance(BinaryTreeNode* Root, int& maxLeft, int& maxRight)
{
if (Root == NULL)
{
maxLeft = 0;
maxRight = 0;
return 0;
}
int maxLL, maxLR, maxRL, maxRR;
int maxDistLeft, maxDisRight;
if (Root->m_pLeft != NULL)
{
maxDistLeft = GetMaxDistance(Root->m_pLeft, maxLL, maxLR);
maxLeft = max(maxLL, maxLR) + 1;
}
else
{
maxDistLeft = 0;
maxLeft = 0;
}
if (Root->m_pRight != NULL)
{
maxDisRight = GetMaxDistance(Root->m_pRight, maxRL, maxRR);
maxRight = max(maxRL, maxRR) + 1;
}
else
{
maxDisRight = 0;
maxRight = 0;
}
}
bool IsCompleteBinaryTree(BinaryTreeNode* Root)
{
if (Root == NULL)
{
return false;
}
queue<BinaryTreeNode*> q;
q.push(Root);
bool mustHaveNoChild = false;
bool result = true;
while (!q.empty())
{
BinaryTreeNode* Node = q.front();
q.pop();
if (mustHaveNoChild)
{
if (Node->m_pLeft!=NULL||Node->m_pRight!=NULL)
{
retuslt = false;
break;
}
else
{
if (Node->m_pLeft!=NULL && Node->m_pRight!= NULL)
{
q.push(Node->m_pLeft);
q.push(Node->m_pRight);
}
else if (Node->m_pLeft != NULL && Node->m_pRight == NULL)
{
mustHaveNoChild = true;
q.push(Node->m_pLeft);
}
esle if (Node->m_pLeft == NULL && Node->m_pRight != NULL)
{
result = false;
break;
}
else
{
mustHaveNoChild = true;
}
}
}
}
return false;
}
BinaryTreeNode* reBuildingTree(int *PreOder, int* InOder, int nodeNum)
{
if (PreOder == NULL || InOder ==NULL ||nodeNum <= 0)
{
return NULL;
}
BinaryTreeNode* Root = new BinaryTreeNode;
Root->m_nValue = PreOder[0];
Root->m_pLeft = NULL;
Root->m_pRight = NULL;
int rootPositionInorder = -1;
for (int i = 0; i < nodeNum; i++)
{
if (InOder[i] == Root->m_nValue)
{
rootPositionInorder = i;
break;
}
}
if (rootPositionInorder == -1)
{
throw std::exception("Invalid input");
}
int nodeNumLeft = rootPositionInorder;
int *preoderLeft = PreOder + 1;
int *inoderLeft = InOder;
Root->m_pLeft = reBuildingTree(preoderLeft, inoderLeft, nodeNumLeft);
int nodeNumRight = nodeNum - nodeNumLeft - 1;
int *preoderright = PreOder + 1 + nodeNumLeft;
int *inorderright = InOder + nodeNumLeft + 1;
Root->m_pRight = reBuildingTree(preoderright, inorderright, nodeNumRight);
return Root;
}