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 ...
随机推荐
- gym100923C. Por Costel and Bujor (高斯消元)
题意:简化一下 就是解N个 系数矩阵一样 等式右边列矩阵不一样的方程组 题解:系数矩阵一样 为什么我却毫无办法???? 其实只要把等式右边的矩阵都排在后面就好了啊 就变成解一个N x 2N的方程组了 ...
- AtCoder Beginner Contest 164
比赛链接:https://atcoder.jp/contests/abc164 A - Sheep and Wolves #include <bits/stdc++.h> using na ...
- hdu 6814 Tetrahedron 规律+排列组合逆元
题意: 给你一个n,你需要从1到n(闭区间)中选出来三个数a,b,c(可以a=b=c),用它们构成一个直角四面体的三条棱(可看图),问你从D点到下面的三角形做一条垂线h,问你1/h2的期望 题解: 那 ...
- hdu3506 Monkey Party
Problem Description Far away from our world, there is a banana forest. And many lovely monkeys live ...
- win7 & centos7 双系统安装方法
1.准备 1)Centos7镜像 官方:https://www.centos.org/ 阿里镜像:http://mirrors.aliyun.com/centos/ 2)安装windows7系统的电脑 ...
- WSL2 使用Docker运行.NET Core
Docker的安装在前面说过了,此处就不说了,我们检查一下版本: 步入正题. 首先,我们为项目创建Dockerfile(无扩展名) 确保Docker是启动状态: 构建镜像,注意名称必须是全部小写(此处 ...
- uni-app 实战-打包 📦 App
uni-app 实战-打包 App Android & iOS 证书 广告 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问 ...
- TypeScript tuple 元组
TypeScript tuple 元组 元组类型允许您用固定数量的元素表示数组,这些元素的类型是已知的,但不必相同. "use strict"; /** * * @author x ...
- hackr.io & Programming Courses & Programming Tutorials
hackr.io & Programming Courses & Programming Tutorials the Best Programming Courses & Tu ...
- android adb命令,向开发手机添加文件
adb文档 把本地文件发送到调试手机 C:\Users\ajanuw>adb push C:\Users\ajanuw\Music\j.mp3 /storage/emulated/0/Downl ...