#include <iostream>
#include <assert.h>
#include <queue>
using namespace std;
struct BiTreeNode{
int m_nValue;
BiTreeNode* m_pLeft;
BiTreeNode* m_pRight;
} ;
void createTree(BiTreeNode* &root,int value){
if(root==NULL){
root=new BiTreeNode;
root->m_nValue=value;
root->m_pLeft=NULL;
root->m_pRight=NULL;
return;
}
if(value<root->m_nValue)
createTree(root->m_pLeft,value);
else
createTree(root->m_pRight,value);
}
void printInOrder(BiTreeNode *root){
if(root==NULL)
return;
printInOrder(root->m_pLeft);
cout<<root->m_nValue<<" ";
printInOrder(root->m_pRight);
}
void swapTree(BiTreeNode* &root){
if(root==NULL)
return;
BiTreeNode* temp=root->m_pLeft;
root->m_pLeft=root->m_pRight;
root->m_pRight=temp;
if(root->m_pLeft!=NULL)
swapTree(root->m_pLeft);
if(root->m_pRight!=NULL)
swapTree(root->m_pRight);
}
void main(){
BiTreeNode* root=NULL;
createTree(root,5);
createTree(root,3);
createTree(root,4);
createTree(root,7);
createTree(root,2);
createTree(root,6);
createTree(root,1);
printInOrder(root);
cout<<endl;
swapTree(root);
printInOrder(root);
cout<<endl;
system("pause");
}
输入一颗二元查找树,将该树转换为它的镜像
最新推荐文章于 2016-10-19 23:19:11 发布