二叉树镜像之递归、栈、队列实现
来源:互联网 发布:4g 高速网络 编辑:程序博客网 时间:2024/06/06 00:57
#include<iostream>
#include<stack>
#include<deque>
#define N 10
using namespace std;
struct BTnode{
int value;
BTnode* left;
BTnode* right;
};
BTnode *mirror(BTnode *root){
if(!root)
return NULL;
BTnode *tempLeft=root->left;
root->left=root->right;
root->right=tempLeft;
if(root->left)
mirror(root->left);
if(root->right)
mirror(root->right);
return root;
}
BTnode *mirror_stack(BTnode* root){
if(!root)
return NULL;
stack<BTnode *>sta;
sta.push(root);
while(sta.size()){
BTnode *temp=sta.top();
sta.pop();
BTnode* tempLeft=temp->left;
temp->left=temp->right;
temp->right=tempLeft;
if(temp->left)
sta.push(temp->left);
if(temp->right)
sta.push(temp->right);
}
return root;
}
BTnode *mirror_deque(BTnode* root){
if(!root)
return NULL;
deque<BTnode *>deq;
deq.push_back(root);
while(deq.size()){
BTnode *temp=deq.front();
deq.pop_front();
BTnode* tempLeft=temp->left;
temp->left=temp->right;
temp->right=tempLeft;
if(temp->left)
deq.push_back(temp->left);
if(temp->right)
deq.push_back(temp->left);
}
return root;
}
void LDRshowBT(BTnode* root){
cout<<root->value<<" ";
if(root->left)
LDRshowBT(root->left);
if(root->right)
LDRshowBT(root->right);
}
int main(){
BTnode *arr[N+1];
for(int i=1;i<=N;i++){
arr[i]=(BTnode*)malloc(sizeof(BTnode));
arr[i]->value=i;
arr[i]->left=NULL;
arr[i]->right=NULL;
}
for(int i=1;i<=N/2;i++){
arr[i]->left=arr[2*i];
if(2*i+1<=N)
arr[i]->right=arr[2*i+1];
}
LDRshowBT(arr[1]);
cout<<endl;
LDRshowBT(mirror_deque(arr[1]));
cout<<endl;
LDRshowBT(mirror_stack(arr[1]));
cout<<endl;
LDRshowBT(mirror(arr[1]));
return 0;
}
#include<stack>
#include<deque>
#define N 10
using namespace std;
struct BTnode{
int value;
BTnode* left;
BTnode* right;
};
BTnode *mirror(BTnode *root){
if(!root)
return NULL;
BTnode *tempLeft=root->left;
root->left=root->right;
root->right=tempLeft;
if(root->left)
mirror(root->left);
if(root->right)
mirror(root->right);
return root;
}
BTnode *mirror_stack(BTnode* root){
if(!root)
return NULL;
stack<BTnode *>sta;
sta.push(root);
while(sta.size()){
BTnode *temp=sta.top();
sta.pop();
BTnode* tempLeft=temp->left;
temp->left=temp->right;
temp->right=tempLeft;
if(temp->left)
sta.push(temp->left);
if(temp->right)
sta.push(temp->right);
}
return root;
}
BTnode *mirror_deque(BTnode* root){
if(!root)
return NULL;
deque<BTnode *>deq;
deq.push_back(root);
while(deq.size()){
BTnode *temp=deq.front();
deq.pop_front();
BTnode* tempLeft=temp->left;
temp->left=temp->right;
temp->right=tempLeft;
if(temp->left)
deq.push_back(temp->left);
if(temp->right)
deq.push_back(temp->left);
}
return root;
}
void LDRshowBT(BTnode* root){
cout<<root->value<<" ";
if(root->left)
LDRshowBT(root->left);
if(root->right)
LDRshowBT(root->right);
}
int main(){
BTnode *arr[N+1];
for(int i=1;i<=N;i++){
arr[i]=(BTnode*)malloc(sizeof(BTnode));
arr[i]->value=i;
arr[i]->left=NULL;
arr[i]->right=NULL;
}
for(int i=1;i<=N/2;i++){
arr[i]->left=arr[2*i];
if(2*i+1<=N)
arr[i]->right=arr[2*i+1];
}
LDRshowBT(arr[1]);
cout<<endl;
LDRshowBT(mirror_deque(arr[1]));
cout<<endl;
LDRshowBT(mirror_stack(arr[1]));
cout<<endl;
LDRshowBT(mirror(arr[1]));
return 0;
}
- 二叉树镜像之递归、栈、队列实现
- 二叉树(利用栈和队列实现递归和非递归遍历,构建等)
- 遍历二叉树——递归和非递归(栈和队列的应用)实现
- 二叉树遍历之非递归实现
- 数据结构之二叉树的递归实现
- 优先队列之二叉堆(JAVA实现)
- 二叉树前中后序遍历的递归版本和非递归版本、队列实现的层次遍历
- 二叉树镜像,递归和循环实现
- 二叉树镜像的非递归实现
- 递归实现二叉树
- 二叉树,递归实现
- 栈,队列和二叉树的实现
- 二叉队列的实现
- 面试题之二叉树镜像(递归,非递归),深度
- 【递归】 之 二叉树
- 二叉树遍历之栈和队列
- 数据结构之二叉树(附栈实现先序,中序非递归遍历)
- 树——二叉树的遍历之递归实现
- 指责下属最不可取的七种方式
- 第三章 Smith圆图
- TreeSet排序问题
- JavaSE——Java集合(II)
- 彻底退出android程序的方法
- 二叉树镜像之递归、栈、队列实现
- zoj1372
- 第四五六章 多端口网络和散射参量
- windows下 Mysql 错误1067 Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
- 第七章 射频滤波器
- 新上任主管10个常犯的错误
- noj 布线问题
- 千万级并发实现的秘密:内核不是解决方案,而是问题所在!
- ComboBox配置数据源需要注意的一个问题