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. 【noi 2.6_2000】&【poj 2127】 最长公共子上升序列 (DP+打印路径)

    由于noi OJ上没有Special Judge,所以我是没有在这上面AC的.但是在POJ上A了. 题意如标题. 解法:f[i][j]表示a串前i个和b串前j个且包含b[j]的最长公共上升子序列长度 ...

  2. Poj-3922 A simple stone game(k倍动态减法)

    题意: 游戏是这样的:两个玩家以一堆n个石头开始游戏.他们轮流从石堆里取石头,每次至少取一块.先走的人第一步最多可以拿n-1块石头.从那时起,一个玩家最多可以拿k倍于他的对手上次拿的石头.例如,如果一 ...

  3. hdu5497 Inversion

    Problem Description You have a sequence {a1,a2,...,an} and you can delete a contiguous subsequence o ...

  4. Codeforces Round #633 div2 A~C

    A. Filling Diamonds 题意:给你n个菱形方块,问能构成图示形状的有多少种 题解:自己画几个不难发现答案是n 代码: 1 #include <iostream> 2 #in ...

  5. URAL - 1029 dp

    题意: n层楼,每层楼有m个房间.找出一个路径从第一层到达第M层,使得路径上的所有数的和是所有可达路径中最小的,每次上到下一层以后就不能再上去,依次输出路径上的各点在所在层的列数. 题解: 参考链接: ...

  6. Codeforces Round #540 (Div. 3) D2. Coffee and Coursework (Hard Version) (二分,贪心)

    题意:有\(n\)个数,每次可以选\(k(1\le k\le n)\)个数,并且得到\(a_1+max(0,a_2-1)+max(0,a_3-2)+...+max(0,a_k-k+1)\)的贡献,问最 ...

  7. String常用方法介绍

    转载 int length(); 语法:字符串变量名.length(); 返回值为 int 类型.得到一个字符串的字符个数(中.英.空格.转义字符皆为字符,计入长度). String a=" ...

  8. 国产网络测试仪MiniSMB - 如何配置VLAN数据流

    国产网络测试仪MiniSMB(www.minismb.com)是复刻smartbits的IP网络性能测试工具,是一款专门用于测试智能路由器,网络交换机的性能和稳定性的软硬件相结合的工具.可以通过此以太 ...

  9. 苹果证书p12和描述文件的创建方法

    在2020年之前,我们在使用香蕉云编创建苹果证书的时候,只需要注册苹果开发者账号,但不需要缴费成为开发者. 在2020年之后,需要先缴费成为苹果开发者. 假如你还没有注册苹果开发者账号,可以先参考下下 ...

  10. SpringSecurity认证流程

    SpringSecurity配置 SecurityConfig.java @Override protected void configure(HttpSecurity http) throws Ex ...