题目描述 Description

求一棵二叉树的前序遍历,中序遍历和后序遍历

输入描述 Input Description

第一行一个整数n,表示这棵树的节点个数。

接下来n行每行2个整数L和R。第i行的两个整数Li和Ri代表编号为i的节点的左儿子编号和右儿子编号。

输出描述 Output Description

输出一共三行,分别为前序遍历,中序遍历和后序遍历。编号之间用空格隔开。

样例输入 Sample Input

5

2 3

4 5

0 0

0 0

0 0

样例输出 Sample Output

1 2 4 5 3

4 2 5 1 3

4 5 2 3 1

数据范围及提示 Data Size & Hint

n <= 16

分类标签 Tags 点此展开

 

非结构体版x

 #include<iostream>

 using namespace std;

 int tree[][]= {};

 void preorder(int node, int j) { //先序遍历
if(tree[node][j]==) return; cout<<tree[node][j]<<" ";
preorder(tree[node][j], );
preorder(tree[node][j], );
} void inorder(int node, int j) { //中序遍历
if(tree[node][j]==) return; inorder(tree[node][j], );
cout<<tree[node][j]<<" ";
inorder(tree[node][j], );
} void postorder(int node, int j) { //后序遍历
if(tree[node][j]==) return; postorder(tree[node][j], );
postorder(tree[node][j], ); cout<<tree[node][j]<<" ";
} int main() {
int n;//n<=16
cin>>n;
int i, j;
tree[][]=tree[][]=; for (i = ; i <= n; ++i)
for(j=; j<; j++)
cin>>tree[i][j]; preorder(, );
cout<<endl;
inorder(, );
cout<<endl;
postorder(, );
cout<<endl;
return ;
}

结构体版x

 #include<iostream>
#include<cstdio>
#include<cstring> using namespace std; struct node {
int l,r;
} t[]; void qian(int root) {
if(!root)return;
printf("%d ",root);
qian(t[root].l);
qian(t[root].r);
} void zhong(int root) {
if(!root)return;
zhong(t[root].l);
printf("%d ",root);
zhong(t[root].r);
}
void hou(int root) {
if(!root)return;
hou(t[root].l);
hou(t[root].r);
printf("%d ",root);
} int main() {
int n,x,y;
scanf("%d",&n);
for(int i=; i<=n; i++) {
scanf("%d%d",&x,&y);
t[i].l=x;
t[i].r=y;
}
qian();
cout<<endl;
zhong();
cout<<endl;
hou();
cout<<endl;
return ;
}

二叉树的序遍历x(内含结构体与非结构体版x)的更多相关文章

  1. 二叉树中序遍历 (C语言实现)

    在计算机科学中,树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构.二叉树是每个节点最多有两个子树的有序树.通常子树被称作“左子树”(left subtre ...

  2. LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard

    题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...

  3. codevs3143 二叉树的序遍历

    难度等级:白银 3143 二叉树的序遍历 题目描述 Description 求一棵二叉树的前序遍历,中序遍历和后序遍历 输入描述 Input Description 第一行一个整数n,表示这棵树的节点 ...

  4. 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)

    题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...

  5. 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

    题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...

  6. 二叉树后序遍历的非递归算法(C语言)

    首先非常感谢‘hicjiajia’的博文:二叉树后序遍历(非递归) 这篇随笔开启我的博客进程,成为万千程序员中的一员,坚持走到更远! 折磨了我一下午的后序遍历中午得到解决,关键在于标记右子树是否被访问 ...

  7. python数据结构之树和二叉树(先序遍历、中序遍历和后序遍历)

    python数据结构之树和二叉树(先序遍历.中序遍历和后序遍历) 树 树是\(n\)(\(n\ge 0\))个结点的有限集.在任意一棵非空树中,有且只有一个根结点. 二叉树是有限个元素的集合,该集合或 ...

  8. [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  9. 左神算法基础班4_1&2实现二叉树的先序、中序、后序遍历,包括递归方式和非递归

    Problem: 实现二叉树的先序.中序.后序遍历,包括递归方式和非递归方式 Solution: 切记递归规则: 先遍历根节点,然后是左孩子,右孩子, 根据不同的打印位置来确定中序.前序.后续遍历. ...

随机推荐

  1. Codeforces Round #309 (Div. 1)

    A. Kyoya and Colored Balls 大意: 给定$k$种颜色的球, 第$i$种颜色有$c_i$个, 一个合法的排列方案满足最后一个第$i$种球的下一个球为第$i+1$种球, 求合法方 ...

  2. IaaS、PaaS、SaaS是云计算的三种服务模式

    IaaS.PaaS.SaaS是云计算的三种服务模式 1. SaaS:Software-as-a-Service(软件即服务)提供给客户的服务是运营商运行在云计算基础设施上的应用程序,用户可以在各种设备 ...

  3. 阿里云语音合成(汉语英语)带UI界面的小程序(python)

    一,项目说明 将汉文转汉语.英文转英语,同时又有逗号<###English###>,<,,,>和句号<...>标志符用于文件处理.其中英文包含在### 英文 ### ...

  4. squoosh

    谷歌在线压缩图片

  5. C#取绝对值函数

    System.Math.Abs(float value); System.Math.Abs(decimal value); System.Math.Abs(int value); System.Mat ...

  6. 【原创】大叔经验分享(73)scala akka actor

    import java.util.concurrent.{ExecutorService, Executors, TimeUnit} import akka.actor.{Actor, ActorSy ...

  7. 【原创】大叔经验分享(57)hue启动coordinator时报错

    hue启动coordinator时报错,页面返回undefinied错误框: 后台日志报错: runcpserver.log [13/May/2019 04:34:55 -0700] middlewa ...

  8. 【原创】大叔经验分享(56)hue导出行数限制

    /opt/cloudera/parcels/CDH/lib/hue/apps/beeswax/src/beeswax/conf.py # Deprecated DOWNLOAD_CELL_LIMIT ...

  9. 【weixi】微信支付---微信公众号JSAPI支付

    一.JSAPI支付 JSAPI支付是用户在微信中打开商户的H5页面,商户在H5页面通过调用微信支付提供的JSAPI接口调起微信支付模块完成支付.应用场景有: ◆ 用户在微信公众账号内进入商家公众号,打 ...

  10. 移动端真机debug调试神器 vConsole学习(一)之基础

    参考 使用方法 移动端真机debug调试神器 vConsole的引入说明(原生态与WebPack) 移动端使用vconsole调试console vConsole ——开源的前端 console 调试 ...