一、求二叉樹深度node
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); }
二、求二叉樹總節點數ui
int GetNodeNum(BinaryTreeNode * pRoot) { if(pRoot == NULL) // 遞歸出口 return 0; return GetNodeNum(pRoot->m_pLeft) + GetNodeNum(pRoot->m_pRight) + 1; }
三、求二叉樹葉子數code
int GetLeafNodeNum(BinaryTreeNode * pRoot) { if(pRoot == NULL) return 0; if(pRoot->m_pLeft == NULL && pRoot->m_pRight == NULL) return 1; int numLeft = GetLeafNodeNum(pRoot->m_pLeft); // 左子樹中葉節點的個數 int numRight = GetLeafNodeNum(pRoot->m_pRight); // 右子樹中葉節點的個數 return (numLeft + numRight); }
四、求k層節點個數遞歸
int GetNodeNumKthLevel(BinaryTreeNode * pRoot, int k) { if(pRoot == NULL || k < 1) return 0; if(k == 1) return 1; int numLeft = GetNodeNumKthLevel(pRoot->m_pLeft, k-1); // 左子樹中k-1層的節點個數 int numRight = GetNodeNumKthLevel(pRoot->m_pRight, k-1); // 右子樹中k-1層的節點個數 return (numLeft + numRight); }
五、二叉樹遍歷(前序遍歷)input
void PreOrderTraverse(BinaryTreeNode * pRoot) { if(pRoot == NULL) return; Visit(pRoot); // 訪問根節點 PreOrderTraverse(pRoot->m_pLeft); // 前序遍歷左子樹 PreOrderTraverse(pRoot->m_pRight); // 前序遍歷右子樹 }
六、分層遍歷it
void LevelTraverse(BinaryTreeNode * pRoot) { if(pRoot == NULL) return; queue<BinaryTreeNode *> q; q.push(pRoot); 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; }
七、求二叉樹鏡像io
BinaryTreeNode * Mirror(BinaryTreeNode * pRoot) { if(pRoot == NULL) // 返回NULL return NULL; BinaryTreeNode * pLeft = Mirror(pRoot->m_pLeft); // 求左子樹鏡像 BinaryTreeNode * pRight = Mirror(pRoot->m_pRight); // 求右子樹鏡像 // 交換左子樹和右子樹 pRoot->m_pLeft = pRight; pRoot->m_pRight = pLeft; return pRoot; }
八、判斷兩棵二叉樹結構是否相同class
bool StructureCmp(BinaryTreeNode * pRoot1, BinaryTreeNode * pRoot2) { if(pRoot1 == NULL && pRoot2 == NULL) // 都爲空,返回真 return true; else if(pRoot1 == NULL || pRoot2 == NULL) // 有一個爲空,一個不爲空,返回假 return false; bool resultLeft = StructureCmp(pRoot1->m_pLeft, pRoot2->m_pLeft); // 比較對應左子樹 bool resultRight = StructureCmp(pRoot1->m_pRight, pRoot2->m_pRight); // 比較對應右子樹 return (resultLeft && resultRight); }
九、判斷二叉樹是否是平衡二叉樹二叉樹
bool IsAVL(BinaryTreeNode * pRoot, int & height) { if(pRoot == NULL) // 空樹,返回真 { height = 0; return true; } int heightLeft; bool resultLeft = IsAVL(pRoot->m_pLeft, heightLeft); int heightRight; bool resultRight = IsAVL(pRoot->m_pRight, heightRight); if(resultLeft && resultRight && abs(heightLeft - heightRight) <= 1) // 左子樹和右子樹都是AVL,而且高度相差不大於1,返回真 { height = max(heightLeft, heightRight) + 1; return true; } else { height = max(heightLeft, heightRight) + 1; return false; } }
十、由前序遍歷序列和中序遍歷序列重建二叉樹exception
BinaryTreeNode * RebuildBinaryTree(int* pPreOrder, int* pInOrder, int nodeNum) { if(pPreOrder == NULL || pInOrder == NULL || nodeNum <= 0) return NULL; BinaryTreeNode * pRoot = new BinaryTreeNode; // 前序遍歷的第一個數據就是根節點數據 pRoot->m_nValue = pPreOrder[0]; pRoot->m_pLeft = NULL; pRoot->m_pRight = NULL; // 查找根節點在中序遍歷中的位置,中序遍歷中,根節點左邊爲左子樹,右邊爲右子樹 int rootPositionInOrder = -1; for(int i = 0; i < nodeNum; i++) if(pInOrder[i] == pRoot->m_nValue) { rootPositionInOrder = i; break; } if(rootPositionInOrder == -1) { throw std::exception("Invalid input."); } // 重建左子樹 int nodeNumLeft = rootPositionInOrder; int * pPreOrderLeft = pPreOrder + 1; int * pInOrderLeft = pInOrder; pRoot->m_pLeft = RebuildBinaryTree(pPreOrderLeft, pInOrderLeft, nodeNumLeft); // 重建右子樹 int nodeNumRight = nodeNum - nodeNumLeft - 1; int * pPreOrderRight = pPreOrder + 1 + nodeNumLeft; int * pInOrderRight = pInOrder + nodeNumLeft + 1; pRoot->m_pRight = RebuildBinaryTree(pPreOrderRight, pInOrderRight, nodeNumRight); return pRoot; }