- /*
- copyright@nciaebupt 轉載出處:http://blog.csdn.net/nciaebupt/article/details/8506038
- 題目:輸入一顆二元查找樹,將該樹轉換爲它的鏡像,即在轉換後的二元查找樹中,左子樹的結點都大於右子樹的結點。 用遞歸和循環兩種方法完成樹的鏡像轉換。
- 例如:
- 8 8
- / \ 轉換 / \
- 6 10 --> 10 6
- / \ / \ / \ / \
- 5 7 9 11 11 9 7 5
- */
- #include <cstdlib>
- #include <iostream>
- #include <stack>
- struct BSTreeNode
- {
- int value;
- int flag;
- BSTreeNode * left;
- BSTreeNode * right;
- };
- //創建二叉查找樹
- BSTreeNode * InsertBSTree(BSTreeNode * pRoot,int value)
- {
- //創建節點並初始化
- BSTreeNode * pNode = new BSTreeNode();
- pNode->value = value;
- pNode->flag = 0;
- pNode->left = NULL;
- pNode->right = NULL;
- BSTreeNode * pCur = pRoot;
- BSTreeNode * pParent = NULL;
- if(pRoot == NULL)
- {
- pRoot = pNode;
- }
- else
- {
- while(pCur != NULL)
- {
- pParent = pCur;
- if(value < pCur->value)
- pCur = pCur->left;
- else
- pCur = pCur->right;
- }
- if(value < pParent->value)
- pParent->left = pNode;
- else
- pParent->right = pNode;
- }
- return pRoot;
- }
- //中序遍歷二叉樹
- void InOrderBSTree1(BSTreeNode * pRoot)
- {
- BSTreeNode * pCur = pRoot;
- if(pCur != NULL)
- {
- InOrderBSTree1(pCur->left);
- std::cout<<pCur->value<<std::endl;
- InOrderBSTree1(pCur->right);
- }
- }
- void InOrderBSTree(BSTreeNode * pRoot)
- {
- BSTreeNode cur = *pRoot;
- std::stack<BSTreeNode> stack;
- stack.push(cur);
- while(!stack.empty())
- {
- if(stack.top().flag == 0)
- {
- stack.top().flag = 1;
- if(stack.top().left != NULL)
- {
- stack.push(*(stack.top().left));
- }
- }
- else
- {
- std::cout<<stack.top().value<<std::endl;
- BSTreeNode tmp = stack.top();
- stack.pop();
- if(tmp.right != NULL)
- {
- stack.push(*(tmp.right));
- }
- }
- }
- }
- //將二叉查找樹轉換爲它的鏡像,遞歸實現
- void BSTree2MirrorRcur(BSTreeNode * pRoot)
- {
- if(pRoot == NULL)
- {
- return;
- }
- //交換指針
- BSTreeNode * tmp = pRoot->left;
- pRoot->left = pRoot->right;
- pRoot->right = tmp;
- //遞歸左子樹
- if(pRoot->left != NULL)
- {
- BSTree2MirrorRcur(pRoot->left);
- }
- //遞歸右子樹
- if(pRoot->right != NULL)
- {
- BSTree2MirrorRcur(pRoot->right);
- }
- }
- //將二叉查找樹轉換爲它的鏡像,循環實現
- void BSTree2Mirror(BSTreeNode * pRoot)
- {
- if(pRoot == NULL)
- return;
- std::stack<BSTreeNode*> pStack;
- BSTreeNode * pCur = pRoot;
- BSTreeNode * pTmp = NULL;
- pStack.push(pCur);
- while(!pStack.empty())
- {
- pCur = pStack.top();
- pStack.pop();
- pTmp = pCur->left;
- pCur->left = pCur->right;
- pCur->right = pTmp;
- if(pCur->left != NULL)
- pStack.push(pCur->left);
- if(pCur->right != NULL)
- pStack.push(pCur->right);
- }
- }
- int main(int args,char **argv)
- {
- int array[] = {8,6,10,5,7,9,11};
- int len = sizeof(array)/sizeof(int);
- //創建二叉查找樹
- BSTreeNode * pRoot = NULL;
- for(int i = 0;i < len;++i)
- {
- pRoot = InsertBSTree(pRoot,array[i]);
- }
- //中序遍歷二叉樹
- InOrderBSTree(pRoot);
- //將二叉查找樹轉換爲它的鏡像,遞歸實現
- //BSTree2MirrorRcur(pRoot);
- //中序遍歷二叉樹
- //InOrderBSTree(pRoot);
- //將二叉查找樹轉換爲它的鏡像,循環實現
- BSTree2Mirror(pRoot);
- //中序遍歷二叉樹
- InOrderBSTree(pRoot);
- system("pause");
- return 0;
- }
遞歸和循環兩種方法完成樹的鏡像轉換
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.