求二叉树的层次遍历(SDUT 2824)
Problem Description
已知一颗二叉树的前序遍历和中序遍历,求二叉树的层次遍历。
Input
输入数据有多组,输入T,代表有T组测试数据。每组数据有两个长度小于50的字符串,第一个字符串为前序遍历,第二个为中序遍历。
Output
每组输出这颗二叉树的层次遍历。
Sample Input
2
abc
bac
abdec
dbeac
Sample Output
2
abc
bac
abdec
dbeac
/** By Mercury_LC */
/** https://blog.csdn.net/Mercury_Lc */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char data; // 储存字符
struct node *lc, *rc; // 左右节点
};
char preorder[100]; // 前序
char inorder[100]; // 中序
struct node *creat(int len, char *preorder, char *inorder) /* 根据前序中序建立二叉树*/
{
struct node *root;
int i;
if(len == 0) return NULL; // 如果长度为零,则不能建树
root = (struct node*)malloc(sizeof(struct node)); // 申请新的节点
root -> data = preorder[0]; // 前序的顺序第一个一定是根节点
for(i = 0; i < len; i ++) // 寻找中序中到根节点,即现在的这颗树的所有左子树
{
if(inorder[i] == preorder[0])break; // 找到跳出循环
}
root -> lc = creat(i, preorder + 1, inorder); // 建左子树
root -> rc = creat(len - i - 1, preorder + i + 1, inorder + i + 1); // 建右子树
return root; // 返回根节点。
};
void level_traversal(struct node *root) /* 层次遍历*/
{
if(root == NULL) return; // 树不存在
struct node *queue[10005], *now; // 建立队列
int front = 0; // 队首、尾初始化
int rear = 0;
queue[rear ++] = root; // 入队
while(front < rear)
{
now = queue[front ++]; // 出队
printf("%c", now -> data);
if(now -> lc != NULL) // 左子树
{
queue[rear++] = now -> lc;
}
if(now -> rc != NULL) // 右子树
{
queue[rear++] = now -> rc;
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%s%s",preorder,inorder);
struct node *root;
int len = strlen(preorder);
root = creat(len,preorder,inorder);
level_traversal(root);
printf("\n");
}
return 0;
}
求二叉树的层次遍历(SDUT 2824)的更多相关文章
- 数据结构实验之求二叉树后序遍历和层次遍历(SDUT 2137)
Problem Description 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历和层序遍历. Input 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据. ...
- LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- lintcode : 二叉树的层次遍历II
题目 二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, ...
- lintcode : 二叉树的层次遍历
题目 二叉树的层次遍历 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历 ...
- LintCode 二叉树的层次遍历 II
中等 二叉树的层次遍历 II 查看执行结果 42% 通过 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 您在真实的面试中是否遇到过这个 ...
- 【leetcode-102,107,103】 二叉树的层次遍历
102. 二叉树的层次遍历 (1过,隐蔽错误花时间很多,简单题目本应很快,下次注意红色错误的地方) 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: ...
- 107. 二叉树的层次遍历 II
107. 二叉树的层次遍历 II 题意 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历). 解题思路 递归:利用前序遍历的思想,在递归过程中 ...
- Leetcode 102 二叉树的层次遍历 Python
二叉树的层次遍历 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 ...
- LeetCode 107 ——二叉树的层次遍历 II
1. 题目 2. 解答 与 LeetCode 102 --二叉树的层次遍历 类似,我们只需要将每一层的数据倒序输出即可. 定义一个存放树中数据的向量 data,一个存放树的每一层数据的向量 level ...
随机推荐
- 20190804-Python基础 第二章 列表和元组
容器,Python支持一种数据结构的基本概念(容器,基本上就是可包含其他对象的对象.) 两种主要的容器是:序列(如列表和元组)和映射(如字典) Ps: 列表与元组区别,列表可修改,元组不能. 对序列的 ...
- 监控提示message
见文件 监控提示message.rar ---可作时时监控提示功能
- (转)查找算法:二叉排序树(BSTree)
二叉排序树(Binary Sort Tree),又称为二叉查找树(Binary Search Tree) ,即BSTree. 构造一棵二叉排序树的目的,其实并不是为了排序,而是为了提高查找和插入删除的 ...
- 搭建Leanote网络云笔记
下载启动 MongoDB Leanote 依赖 MongoDB 作为数据存储,下面开始安装 MongoDB: 下载 MongoDB 进入 /home 目录,并下载 MongoDB: cd /home ...
- MyEclipse的Server标签出现:Could not create the view: An unexpected exception was thrown
删除工作空间下的.metadata\.plugins\org.eclipse.core.runtime\.settings\com.genuitec.eclipse.ast.deploy.core.p ...
- c#连接数据库SqlHelper报错
这是一个困扰了我好几天的问题,首先看一下报错信息 代码: private static string connectionString = ConfigurationManager.Connectio ...
- Qt之QTableWidget
学习QTableWidget就要首先看看QTableView控件(控件也是有”家世“的!就像研究人一样一样的),因为QTableWidget继承于类QTableView. 两者主要区别是QTableV ...
- LeetCode 腾讯精选50题--二叉树中的最大路径和
二叉树中的最大路径和 题目描述 给定一个非空二叉树,返回器最大路径和,路径指一条从任意节点出发,到达任意节点的序列,该路径至少包含一个节点,且不一定经过根节点 解题思路 树这一类数据结构我还不是很熟悉 ...
- 关于阮一峰老师es6(第三版)中管道机制代码的理解浅析
最近正在学习阮一峰老师的es6(第三版)教材,在学到第七章<函数的扩展>中的箭头函数嵌套时,文中提到了一个关于“管道机制”的示例,文中源代码如下: //es6(第三版)教材中的管道机制源代 ...
- Dart中的匿名方法与自执行方法
void main() { // 匿名方法 var printSomethings = () { print("somethings"); }; printSomethings() ...