题目描述 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. MySQL安装及初级增删改查一

    学习MYsql 是参照这个维C果糖的总结,学习目录网址:https://blog.csdn.net/qq_35246620/article/details/70823903,谢谢大神的无私分享. 一. ...

  2. X-Router软路由设置

    一 内网:     ip   192.168.0.1      掩码  255.255.255.0      网关   (空)     DNS   202.96.128.68(佛山的)手动写入 二 外 ...

  3. bash 中的 :=、=、:-、-、=?、?、:+、+

    bash 中的 :=.=.:-.-.=?.?.:+.+ 来源 https://www.cnblogs.com/fhefh/archive/2011/04/22/2024750.html 变量替换和变量 ...

  4. HttpWorkerRequest应用简介

    1. Using HttpWorkerRequest for getting headers1.使用HttpWorkerRequest获取headers信息 First, the HttpWorker ...

  5. 动画方案 Lottie 学习(一)之基础

    参考 lottie系列文章(一):lottie介绍 lottie系列文章(二):lottie最佳实践 lottie系列文章(三):动画设计规范 lottie系列文章(四):源码分析——svg渲染

  6. 二、maven学习

    maven工程运行环境修改 例如maven自带Tomcat6,我们想使用Tomcat7 pom.xml #配置多一个Tomcat7<plugins> <plugin> < ...

  7. 爱上Java诊断利器之Arthas

    1. Arthas是什么? 摘自Arthas的Github介绍: Arthas is a Java Diagnostic tool open sourced by Alibaba. Arthas al ...

  8. 关于javascript的事件执行机制理解

    理解javascript事件执行机制 众所周知,js是一个单线程的语言,这意味着同一时间只能做一件事,但是我们又说js是异步的.首先,单线程并不是没有优点.作为浏览器脚本语言,JavaScript 的 ...

  9. webpack配置不同打包配置

    生成环境与开发环境打包配置 使用package.json配置npm run开启不同的打包配置 ...webpack基本使用最后一篇博客 在上一篇博客中详细的演示了webpack开启本地服务和热更新,这 ...

  10. JS-数值转换

    JS数值转换 JS数值转换的方式有4种:Number(),parseInt(),parseFloat(),数据类型*1或者/1. 他们的区别在于: 1.Number():可以将非数值转为数值 如果是B ...