标签:二叉树的映象
很自然想起来递归:
代码:
#include <iostream> #include <list> using namespace std; typedef struct tree1{ int data; struct tree1 * lchild; struct tree1 * rchild; }Tree,* pTree; void createTree(pTree & p){ int temp ; scanf("%d",&temp); if(temp != 0){ p=(pTree)malloc(sizeof(Tree)); p->data = temp; createTree(p->lchild); createTree(p->rchild); }else{ p = NULL; } } void print(pTree p){ if(p){ cout<<p->data<<" "; print(p->lchild); print(p->rchild); }else return; } void mirrorRever(pTree &p){ //当为空树 或只有一个节点 if(NULL == p || NULL == p->lchild && NULL == p->rchild){ return; } pTree temp = p->lchild; p->lchild = p->rchild; p->rchild = temp; if(p->lchild != NULL) mirrorRever(p->lchild); if(p->rchild != NULL) mirrorRever(p->rchild); } int main() { pTree pHead = NULL; createTree(pHead); cout <<endl; print(pHead); cout <<endl<< "翻转"<<endl; mirrorRever(pHead); print(pHead); return 0; }
标签:二叉树的映象
原文地址:http://blog.csdn.net/buyingfei8888/article/details/38361405