题目描述 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. python 基础(十九)--re正则表达式模块

    正则表达式模式 模式 描述 ^ 匹配字符串的开头 $ 匹配字符串的末尾. . 匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符. [...] 用来表示一组字符 ...

  2. hype-v上centos7部署高可用kubernetes集群实践

    概述 在上一篇中已经实践了 非高可用的bubernetes集群的实践 普通的k8s集群当work node 故障时是高可用的,但是master node故障时将会发生灾难,因为k8s api serv ...

  3. 打印从1到最大的n位数(考虑大数问题)

    void Print1ToMaxOfNDigits(int n) { if(n <= 0) { return; } int * number = new int[n]; for(int i = ...

  4. 帝国cms常用标签

    .loop获取时间标签 /*获取年月日,时分秒.可以按照自己的需求单独获取年,或者月.*/ <?=date("Y-m-d H:i:s",$bqr[newstime])?> ...

  5. 输入列号得到excel对应的字母列

    zexcel_cell_column 类型是INT4 FUNCTION ZGET_EXCEL_COL. *"----------------------------------------- ...

  6. OSCP-Kioptrix2014-1 环境搭建

    环境搭建 该系列文章参考 : https://www.youtube.com/watch?v=bWM0BCQ5q1o&list=PL9WW-prbqvGzHsGK_OqTyYWbCZjucpI ...

  7. 基于docker安装pxc集群

    基于docker安装pxc集群 一.PXC 集群的安装 PXC集群比较特殊,需要安装在 linux 或 Docker 之上.这里使用 Docker进行安装! Docker的镜像仓库中包含了 PXC数据 ...

  8. 异步任务——AsyncTask的初步认识

    ProgressBar_test.class package com.example.administrator.ten_9; import android.app.Activity; import ...

  9. qunee 流动的关系

    <!DOCTYPE html> <html> <head> <title>Hello Qunee for HTML5</title> < ...

  10. 8.2.ZooKeeper应用场景

    7.ZooKeeper应用举例 为了方便大家理解ZooKeeper,在此就给大家举个例子,看看ZooKeeper是如何实现的他的服务的,我以ZooKeeper提供的基本服务分布式锁为例. 7.1 分布 ...