题目描述 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. 合并两个排序链表——牛客offer

    题目描述: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 解题思路: 1.一般看到合并这类的题目就会很自然的想到创建一个新的链表,然后将两个链表根据一定 ...

  2. 推荐系统遇上深度学习(十)--GBDT+LR融合方案实战

    推荐系统遇上深度学习(十)--GBDT+LR融合方案实战 0.8012018.05.19 16:17:18字数 2068阅读 22568 推荐系统遇上深度学习系列:推荐系统遇上深度学习(一)--FM模 ...

  3. Java EE javax.servlet.http中的HttpSession接口

    HttpSession接口 public interface HttpSession (https://docs.oracle.com/javaee/7/api/javax/servlet/http/ ...

  4. 2-Perl 环境安装

    1.Perl 环境安装在我们开始学习 Perl 语言前,我们需要先安装 Perl 的执行环境.Perl 可以在以下平台下运行:Unix (Solaris, Linux, FreeBSD, AIX, H ...

  5. oracle按用户导出导入表

    查看备份目录:select * from dba_directories where directory_name='DATA_PUMP_DIR'; 导入导出的文件名默认都是以备份目录为相对路径. 按 ...

  6. javascript——== 和===的区别

    == 等于 === 全等(值和类型) console.log(5==5);//true console.log(5=="5");//true console.log(5===5); ...

  7. linux 阿里云 新机器 安装jdk1.8

    2019年7月17日15:58:34 按着顺序来: wget https://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4f ...

  8. redis的keys常用操作及redis的特性

    redis的keys常用操作 1.获得所有的keys: keys * 2.可以模糊查询 keys:keys  my* 3.删除keys:del mymkey1 mykey2 4.是否存在keys:ex ...

  9. uni app以及小程序 --环境搭建以及编辑器

    https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 根据以上网页下载自己电脑相应的版本的微信开发者工具(目录 ...

  10. 禁用div元素(不可点击)

    style="pointer-events: none;" 可以封装使用 .disable { pointer-events: none; } //禁用 $.fn.disable ...