首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

输入一颗二元查寻树,将该树转换为它的镜像

2013-09-07 
输入一颗二元查找树,将该树转换为它的镜像#include iostream#include assert.h#include queueusing

输入一颗二元查找树,将该树转换为它的镜像

#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);elsecreateTree(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");}

热点排行