二叉树回顾(1):建立、交换左右子树
来源:互联网 发布:电视机电视直播软件 编辑:程序博客网 时间:2024/06/01 09:49
二叉树的建立和中序遍历输出、交换左右子树
由数组建立二叉排序树,并使用中序遍历进行输出,交换二叉树的左右子树(递归写法)
#include <iostream>#include<cstdio>using namespace std;struct node{int data;node *lchild;node *rchild;};node* newNode(int x){node *root=new node;root->data=x;root->lchild=root->rchild=NULL;return root;}void insert(node* &root,int x){if(root==NULL){root=newNode(x);return;}//以下不用新建节点,新建的都是在上面递归边界处 if(root->data<=x){insert(root->rchild,x);}else{insert(root->lchild,x);}}node* create(int a[],int n){node* root=NULL;for(int i=0;i<n;++i){insert(root,a[i]);}return root;}void exchange(node* root){node* temp;if(!root)return;exchange(root->lchild);exchange(root->rchild);temp=root->lchild;root->lchild=root->rchild;root->rchild=temp;}//中序序列输出 void inorder(node* root){if(root==NULL)return;inorder(root->lchild);printf("%d ",root->data);inorder(root->rchild);}int main(){int i,j;int a[]={12,43,13,3,5,1,56,74,10,8};int n=sizeof(a)/sizeof(a[0]);//由数组建立二叉树 node* root=create(a,n);printf("before exchange:\n");inorder(root);//交换左右子树 exchange(root);printf("\nafter excahnge:\n");inorder(root);return 0;}
0 0
- 二叉树回顾(1):建立、交换左右子树
- 二叉树交换左右子树
- 实验四、1二叉树左右子树交换
- 二叉树左右子树交换的算法
- 交换左右子树(二叉树)
- 二叉树左右子树的交换
- 1021_二叉树的复制和左右子树交换
- 226. Invert Binary Tree (交换二叉树左右子树)
- 第4章第1节练习题7 交换二叉树所有节点左右子树
- 二叉树镜像(交换左右子树)
- 二叉树(三)二叉树的高度、交换两个子树左右节点
- 经典算法学习——交换二叉树的左右子树(二叉树的翻转)
- 左右子树交换
- 交换左右子树
- 二叉树的操作(建树,先序遍历,交换左右子树,销毁
- 二叉树中所有节点的左右子树相互交换 递归与非递归程序
- 获取二叉树的叶子数、高度以及左右子树的交换的C语言实现
- 交换二叉树的左右子树——非递归方式
- 关于Joda-time
- Android数据存储
- Mybatis 源码 sql执行流程分析
- Spring与SpringMVC的容器关系分析
- git clone 远程分支
- 二叉树回顾(1):建立、交换左右子树
- sublime工具使用技巧
- mybatis中一对多映射,但两张表id相同怎么办?
- MySQL权限篇之SHOW DATABASES及SHOW VIEW
- 火车票订票系统
- ora 14400
- Android 三大图片缓存原理、特性对比
- Android性能优化典范 - 第3季
- mysql 设置、添加主键以及自增问题