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语言实现+根据中序和前序遍历构建树并且给出后序遍历序列的更多相关文章

  1. PAT 1086 Tree Traversals Again

    PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...

  2. PAT 1086 Tree Traversals Again[中序转后序][难]

    1086 Tree Traversals Again(25 分) An inorder binary tree traversal can be implemented in a non-recurs ...

  3. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  4. 1086 Tree Traversals Again——PAT甲级真题

    1086 Tree Traversals Again An inorder binary tree traversal can be implemented in a non-recursive wa ...

  5. PAT 1020. Tree Traversals

    PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...

  6. PAT 甲级 1086 Tree Traversals Again

    https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...

  7. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  8. PAT A1020 Tree Traversals(25)

    题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

  9. 1086 Tree Traversals Again

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...

随机推荐

  1. CodeForces - 948C (前缀和+二分)

    博客界面的小人搞不好导致无心写博客 题意:tyd非常喜欢玩雪,下雪下了n天,第i天她会堆一堆大小为Vi的雪堆,但因为天气原因,每堆雪会融化Ti,问每天总共融化了多少雪: 直接上代码+注释 1 #inc ...

  2. Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2) D. XOR-gun (二进制,异或,前缀和)

    题意:给你一组非递减的数,你可以对两个连续的数进行异或,使其合并为一个数,问最少操作多少次使得这组数不满足非递减. 题解:首先,给出的这组数是非递减的,我们考虑二进制,对于三个连续的非递减的最高位相同 ...

  3. fzu2200 cleaning

    Problem Description N个人围成一圈在讨论大扫除的事情,需要选出K个人.但是每个人与他距离为2的人存在矛盾,所以这K个人中任意两个人的距离不能为2,他们想知道共有多少种方法.  In ...

  4. C# List.Sort与IComparer接口及Comparison委托应用于集合排序

    C#里List.Sort的用法 using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  5. 【转】分布式事务之——tcc-transaction分布式TCC型事务框架搭建与实战案例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/73731363 一.背景 有一定分布式开发经验的朋友都知道,产品/项目/系统最初为了 ...

  6. 四、Jmeter 集合点(实际场景应用)

    一.jmeter集合点的作用域及作用范围 先明确一些概念:1)定时器是在每个sampler(采样器)之前执行的,而不是之后: 是的,你没有看错,不管这个定时器的位置放在sampler之后,还是之下,它 ...

  7. 二进制安装kubernetes(一) 环境准备及etcd组件安装及etcd管理软件etcdkeeper安装

    实验环境: 架构图: 主机环境: 操作系统:因docker对内核需要,本次部署操作系统全部采用centos7.6(需要内核3.8以上) VM :2C 2G 50G * 5  PS:因后面实验需要向k8 ...

  8. 2017.8.11 think list

    递推式与模数不互质,如何利用中国剩余定理综合答案

  9. Raspberry Pi & GPIO

    Raspberry Pi & GPIO pinout === pin out / p in out pi@raspberrypi:~ $ pinout ,------------------- ...

  10. Flutter for Desktop

    Flutter for Desktop https://flutter.dev/desktop https://github.com/flutter/flutter/wiki/Desktop-shel ...