赞
踩
实现代码如下:
非递归算法:
- //root是根节点
- void swap(){
- BiNode * queue[100], * temp, * root1;
- int first = 0, last = 0;
- queue[first++] = root;
- while(first != last){
- root1 = queue[++last];
- temp = root1->lchild;
- root1->lchild = root1->rchild;
- root1->rchild = temp;
- if(root1->lchild != NULL)
- queue[first++] = root1->lchild;
- if(root1->rchild != NULL)
- queue[first++] = root1->rchild;
- }
- }

递归算法:
- root是根节点
- BiNode * temp, * root1;
- void swap(BiNode root1){
- if(root1 == NULL) return;
- else{
- temp = root1->lchild;
- root1->lchild = root1->rchild;
- root1->rchild = temp;
- swap(root1->lchild);
- swap(root1->rchild);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。