PAT-1086(Tree Traversals Again)Java语言实现+根据中序和前序遍历构建树并且给出后序遍历序列
Tree Traversals Again
Tree Traversals Again
- 这里的第一个tip就是注意到非递归中序遍历的过程中,进栈的顺序恰好是前序遍历的顺序,而出栈的顺序恰好是中序遍历的顺序。
- 第二个需要注意的就是如何根据中序遍历和前序遍历构建出一棵二叉树。
- 第三个是二叉树的后序遍历,这里我采用的是后序遍历的方法
import java.util.Scanner;
import java.util.Stack;
/**
* @Author WaleGarrett
* @Date 2020/9/5 12:02
*/
/**
* 根据前序遍历和中序遍历来构建一棵树
* 进栈的顺序对应前序遍历的顺序,出栈的顺序对应中序遍历的顺序
*/
public class PAT_1086 {
static int[] preorder;
static int[] inorder;
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
Stack<Integer>sta=new Stack<>();
int n=scanner.nextInt();
scanner.nextLine();//读取上一行的换行符
preorder=new int[n];
inorder=new int[n];
int precnt=0,incnt=0;
n*=2;
while(n!=0){
String s=scanner.nextLine();
// System.out.println(s);
if(s.length()>4){//push操作
String values=s.substring(5);
int value=Integer.parseInt(values);
sta.push(value);
preorder[precnt++]=value;
}else{//pop操作
int value=sta.peek();
sta.pop();
inorder[incnt++]=value;
}
n--;
}
TreeNode root=createTree(0,precnt-1,0,incnt-1);//根据前序遍历序列和中序遍历序列构建二叉树
System.out.println(postOrder(root," ").trim());
}
public static TreeNode createTree(int prel,int prer,int inl,int inr){
if(prel>prer)//叶子结点
return null;
TreeNode root=new TreeNode();
root.value=preorder[prel];
int position=0;
for(int i=inl;i<=inr;i++){
if(preorder[prel]==inorder[i]){
position=i;
break;
}
}
int inlcnt=position-inl;
root.left=createTree(prel+1,prel+inlcnt,inl,position-1);
root.right=createTree(prel+inlcnt+1,prer,position+1,inr);
return root;
}
public static String postOrder(TreeNode root,String result){
if(root.left!=null)
result=postOrder(root.left,result);
if(root.right!=null)
result=postOrder(root.right,result);
result=result+root.value+" ";
return result;
}
}
class TreeNode{
TreeNode left;
TreeNode right;
int value;
public TreeNode(){
left=right=null;
value=-1;
}
public TreeNode(int value){
this.value=value;
left=right=null;
}
}
PAT-1086(Tree Traversals Again)Java语言实现+根据中序和前序遍历构建树并且给出后序遍历序列的更多相关文章
- PAT 1086 Tree Traversals Again
PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 1086 Tree Traversals Again[中序转后序][难]
1086 Tree Traversals Again(25 分) An inorder binary tree traversal can be implemented in a non-recurs ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- 1086 Tree Traversals Again——PAT甲级真题
1086 Tree Traversals Again An inorder binary tree traversal can be implemented in a non-recursive wa ...
- PAT 1020. Tree Traversals
PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...
- PAT 甲级 1086 Tree Traversals Again
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT A1020 Tree Traversals(25)
题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
- 1086 Tree Traversals Again
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...
随机推荐
- 【uva 1515】Pool construction(图论--网络流最小割 模型题)
题意:有一个水塘,要求把它用围栏围起来,每个费用为b.其中,(#)代表草,(.)代表洞,把一个草变成洞需要费用d, 把一个洞变成草需要费用f.请输出合法方案中的最小费用. 解法:(不好理解...... ...
- Codeforces Global Round 12 D. Rating Compression (思维,双指针)
题意:给你一长度为\(n\)的数组,有一长度为\(k\ (1\le k \le n)\)的区间不断从左往右扫过这个数组,总共扫\(n\)次,每次扫的区间长度\(k=i\),在扫的过程中,每次取当前区间 ...
- C# Dictionary(字典)源码解析&效率分析
通过查阅网上相关资料和查看微软源码,我对Dictionary有了更深的理解. Dictionary,翻译为中文是字典,通过查看源码发现,它真的内部结构真的和平时用的字典思想一样. 我们平时用的字典主要 ...
- 列表解析式 -- Python
列表解析的语法:[expr for iter_var in iterable], 它迭代iterable对象的所有条目.其中的expr应用于序列的每个成员,最后的结果值是该表达式产生的列表,迭代变量并 ...
- 手把手教你通过SQL注入盗取数据库信息
目录 数据库结构 注入示例 判断共有多少字段 判断字段显示位置 显示出登录用户和数据库名 查看所有数据库 获取对应数据库的表 获取对应表的字段名称 获取用户密码 SQL注入(SQL Injection ...
- OPENSOURCE - libcurl
本文仅做备份存档,原文地址如下,请点链接进入 https://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html https://www.c ...
- iTerm2终端工具在Mac OS上使用详解
一.概述 因个人工作需要,使用终端工具进行运维和开发工作,但是Mac OS 自带的终端工具使用堡垒机登录配置不了,而且使用CRT等终端工具每次登录堡垒机都需要配置密码,操作起来很麻烦.一直想找一款终端 ...
- 网络安全-企业环境渗透2-wordpress任意文件读&&FFmpeg任意文件读
参考 http://prontosil.club/posts/c08799e1/ 一. 实验名称 企业环境渗透2 二. 实验目的 [实验描述] 操作机的操作系统是kali 进入系统后默认是命令行界面 ...
- oslab oranges 一个操作系统的实现 实验一
实验目的: 搭建基本实验环境,熟悉基本开发与调试工具 对应章节:第一.二章 实验内容: 1.认真阅读章节资料 2.在实验机上安装virtualbox,并安装ubuntu 3.安装ubuntu开发环境, ...
- 51nod1459 带权最短路
1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分 ...