题目描述
二叉树被记录为文件的过程叫作二叉树的序列化,通过文件内容重建原来二叉树的过程叫作二叉树的反序列化,给定一颗二叉树,请将该二叉树先序序列化和层序序列化。
假设序列化的结果字符串为 str,初始时 str = “”,遍历二叉树时,遇到 null 节点,则在 str 的末尾加上 “#!”,否则加上"当前的节点值!"。
输入描述:
第一行输入两个整数 n 和 root,n 表示二叉树的总节点个数,root 表示二叉树的根节点。
以下 n 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)
输出描述:
输出两行分别表示该二叉树的先序序列化和层序序列化
示例1
输入
复制
2 1
1 2 0
2 0 0
输出
复制
1!2!#!#!#!
1!2!#!#!#!
思路: 首先题意给出的是,节点数n 和根节点root_val 的序号,然后接下来是按照先序遍历的顺序给出的每个节点的父节点,左儿子节点,右儿子节点的序号。
#include<iostream>
#include<queue>
using namespace std;
struct Treenode
{
int val;
Treenode *left;
Treenode *right;
Treenode(){};
Treenode(int x_val){ val=x_val ,left=NULL,right=NULL; };
};
void Create_Treenode( int n,Treenode *root )//建树 这里的 n ,也可以取&n也可以不去,因为n并不是判断递归循环结束的条件
{
if( n==0 )
return ;
int x_val,y_val,z_val;//父,左儿,右儿 节点的权重,
cin>>x_val>>y_val>>z_val;
if(y_val!=0 ) //构建左子树
{
Treenode * L=new Treenode(y_val);
root->left=L;
Create_Treenode(--n , L );
}
if( z_val!=0 )
{
Treenode * R=new Treenode(z_val);
root->right=R;
Create_Treenode(--n , R );
}
}
void preorder(Treenode * root)//先序遍历
{
if( root == NULL )
{
cout<<"#!";
return ;
}
cout<<root->val<<"!";
preorder(root->left);
preorder(root->right);
}
void level_order( Treenode * root )
{
queue<Treenode * > Q;
if( root == NULL)
{
cout<<"#!";
return ;
}
Q.push( root );
while( !Q.empty() )
{
Treenode * x=Q.front();
Q.pop();
if( x==NULL )
{
cout<<"#!";
continue; //可要可不要
}
else
{
cout<<x->val<<"!";
Q.push(x->left);
Q.push(x->right);
}
}
}
int main()
{
int n,root_val;
cin>>n>>root_val;
Treenode * root=new Treenode(root_val);
Create_Treenode( n , root );
preorder(root);
cout<<endl;
level_order(root);
return 0;
}
**补充:**下面的代码增加了二叉树的中序遍历和后序遍历,题目本身没有要求。后期优化:把递归写成非递归形式。
自测数据:
4 1
1 2 3
2 4 0
4 0 0
3 0 0
#include<iostream>
#include<queue>
using namespace std;
struct Treenode
{
int val;
Treenode *left;
Treenode *right;
Treenode(){};
Treenode(int x_val){ val=x_val ,left=NULL,right=NULL; };
};
void Create_Treenode( int n,Treenode *root )//建树
{
if( n==0 )
return ;
int x_val,y_val,z_val;//父,左儿,右儿 节点的权重,
cin>>x_val>>y_val>>z_val;
if(y_val!=0 ) //构建左子树
{
Treenode * L=new Treenode(y_val);
root->left=L;
Create_Treenode(--n , L );
}
if( z_val!=0 )//构建右子树
{
Treenode * R=new Treenode(z_val);
root->right=R;
Create_Treenode(--n , R );
}
}
void preorder(Treenode * root)//先序遍历 根左右
{
if( root == NULL )
{
cout<<"#!";
return ;
}
cout<<root->val<<"!";
preorder(root->left);
preorder(root->right);
}
void inorder( Treenode * root)//中序遍历 左根右
{
if( root == NULL )
{
cout<<"#!";
return ;
}
inorder(root->left );
cout<<root->val<<"!";
inorder(root->right);
}
void postorder( Treenode * root)//后序遍历 左右根
{
if( root == NULL )
{
cout<<"#!";
return ;
}
postorder(root->left );
postorder(root->right);
cout<<root->val<<"!";
}
void level_order( Treenode * root )//层序遍历 借助队列
{
queue<Treenode * > Q;
if( root == NULL)
{
cout<<"#!";
return ;
}
Q.push( root );
while( !Q.empty() )
{
Treenode * x=Q.front();
Q.pop();
if( x==NULL )
{
cout<<"#!";
// continue; //可要可不要
}
else
{
cout<<x->val<<"!";
Q.push(x->left);
Q.push(x->right);
}
}
}
int main()
{
int n,root_val;
cin>>n>>root_val;
Treenode * root=new Treenode(root_val);
Create_Treenode( n , root );
// preorder(root);
// cout<<endl;
// level_order(root);
// inorder(root);
postorder(root);
return 0;
}