nyoj756_重建二叉树_先序遍历
重建二叉树
- 描述
- 题目很简单,给你一棵二叉树的后序和中序序列,求出它的前序序列(So easy!)。
- 输入
- 输入有多组数据(少于100组),以文件结尾结束。
每组数据仅一行,包括两个字符串,中间用空格隔开,分别表示二叉树的后序和中序序列(字符串长度小于26,输入数据保证合法)。 - 输出
- 每组输出数据单独占一行,输出对应得先序序列。
- 样例输入
-
ACBFGED ABCDEFG
CDAB CBAD - 样例输出
-
DBACEGF
BCAD - 来源
- 原创
- 上传者
- TC_黄平
- 解题思路:因为之前做过两个输出后序遍历的,所以用之前的方法做的,但递归的时候想错了,结果差点以为这种方法做不出来。
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; char subs[];
char tmp[];
char in[];
int t[]={}; void traverse(int s1,int s2,int n){ if(n==){
printf("%c",tmp[s1]);
return ;
}
if(n<=){
return ;
}
t[s1]=;
int i;
for(i=;tmp[s1]!=in[s2+i];i++); printf("%c",tmp[s1]);
traverse(s1+(n-i),s2,i);//第一个参数应该是s1+(n-i)
traverse(s1+,s2+i+,n-i-);
} int main()
{
while(scanf("%s %s",subs+,in+)!=EOF){
memset(t,,sizeof(t));
int cou=;
int len=strlen(subs+);
for(int i=len;i>;i--){
tmp[cou++]=subs[i];
}
traverse(,,len);
printf("\n");
}
return ;
}
nyoj756_重建二叉树_先序遍历的更多相关文章
- 剑指offer4:重建二叉树(后序遍历)
1. 题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4 ...
- [leetcode]_根据二叉树的先序遍历(后序遍历) + 中序遍历 重建二叉树
题目1:Construct Binary Tree from Preorder and Inorder Traversal 给定一棵二叉树的先序遍历和中序遍历,求重建二叉树. 思路: 1.先序遍历的第 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 数据结构《10》----二叉树 Morris 中序遍历
无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- lintcode:二叉树的中序遍历
题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 ...
- LeetCode(94):二叉树的中序遍历
Medium! 题目描述: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗 ...
- 【LeetCode题解】94_二叉树的中序遍历
目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...
随机推荐
- Linux下vim查看文件名
在vim下编辑时,有时候看不到文件名,不知道编辑的是那个文件,怎么呢,可以按照下面的方法试试. 查看文件名 在正常模式下: :f 或CTRL+G 查看文件的路径 用 :!pwd 可以看当前的详细路径. ...
- Linux启动管理:grub
1.grub中分区表示 Linux 中 /dev/sda1 在grub中为 hd0,0 代表第一个硬盘的第一个分区 Linux中 /dev/sdb3是扩展分区 在grub中为 ...
- CF451B Sort the Array 水题
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...
- twoSum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- dubbo框架----探索-大型系统架构设计(图解)
对于高并发系统的架构要求: 1. 负载均衡 2.高并发 3.高可用 4.面向服务架构 (Dubbo框架使用) 5.分布式缓存 (redis分布式缓存) 6.分布式全文检索 (solr分分布式全文检索) ...
- 知识梳理HTML篇
HTML 浏览器内核: IE:trident Firefox : gecko Safari/chrome : webkit Opera : presto(新 ...
- 【CISP笔记】操作系统安全
账号安全设置 默认管理账号Administrator更名,设置密码(字母.数字.大小写字母.特殊字符,长度在8位以上). 本地安全策略 打开方式 win+R 输入ecpol.msc 账号锁定策略 用户 ...
- R-处理数据对象的实用函数
- 构建spring+mybatis+redis架构时遇到的小异常
异常一: Caused by: java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Lj ...
- 统计学 nested_design 嵌套设计
nested_design 嵌套设计 li_volleyball ,邓邦良 2016年3月6日 嵌套设计 一.基本概念 嵌套设计(nested design)又称为窝设计和套设计,与析因设计的处理不同 ...