zoukankan      html  css  js  c++  java
  • 1086. Tree Traversals Again (25)

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.


    Figure 1

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

    Output Specification:

    For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

    Sample Input:

    6
    Push 1
    Push 2
    Push 3
    Pop
    Pop
    Push 4
    Pop
    Pop
    Push 5
    Push 6
    Pop
    Pop
    

    Sample Output:

    3 4 2 6 5 1


    解题思路:由几个Push的操作可以组成树的先序遍历结果,而Pop的结果是树的中序遍历的结果,利用先序和中序即可构建一颗唯一的树,然后在后序遍历即可求解。

    #include<iostream>
    #include<vector>
    #include<stack>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    struct Tree{
    	int data;
    	Tree* left;
    	Tree* right;
    	Tree(){
    		left=NULL;
    		right=NULL;
    	}
    };
    vector<int>preorder;
    vector<int>inorder;
    int n,cur;
    int find(int key){
    	for(int i=0;i<n;i++){
    		if(inorder[i]==key){
    			return i;
    		}
    	}
    	return -1;
    }
    Tree* create(int start,int end){
    	if(start>end)return NULL;
    	Tree* t=new Tree();
    	t->data=preorder[cur];
    	cur++;
    	int index=find(t->data);
    	if(start!=end){
    		t->left=create(start,index-1);
    		t->right=create(index+1,end);
    	}
    	return t;
    } 
    int flag=0;
    void postorder(Tree* t){
    	if(!t)return;
    	postorder(t->left);
    	postorder(t->right);
    	if(flag==0){
    		printf("%d",t->data);
    		flag=1;
    	}else{
    		printf(" %d",t->data);
    	}
    }
    int main(){
    	scanf("%d",&n);
    	stack<int>s;
    	int i,j;
    	char str[5];
    	int val;
    	for(i=0;i<n*2;i++){
    		scanf("%s",str);
    		if(str[1]=='u'){
    			scanf("%d",&val);
    			s.push(val);
    			preorder.push_back(val);
    		}else{
    			val=s.top();
    			s.pop();
    			inorder.push_back(val);
    		}
    	} 
    	Tree* t=create(0,n-1);
    	postorder(t);
    	printf("
    ");
    	return 0;
    }
    

      




  • 相关阅读:
    WinForm 窗体应用程序(初步)之一
    ADO.NET
    面向对象思想
    数据库原理
    HTML学习总结
    c# 学习心得(2)
    c# 学习心得(1)
    《大话数据结构》读书笔记(2)
    《大话数据结构》读书笔记(1)
    ASP.NET Core学习总结(3)
  • 原文地址:https://www.cnblogs.com/grglym/p/8043599.html
Copyright © 2011-2022 走看看