博客
关于我
1086 Tree Traversals Again
阅读量:423 次
发布时间:2019-03-06

本文共 4550 字,大约阅读时间需要 15 分钟。

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 (≤) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2 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:

6Push 1Push 2Push 3PopPopPush 4PopPopPush 5Push 6PopPop
 

Sample Output:

3 4 2 6 5 1

 

题意:

采用非递归的方式来确定一颗二叉树,然后将这颗二叉树按照后序遍历的方式来输出。

思路:

这道题用到的理论知识就是通过一棵二叉树的先序遍历和中序遍历来构建这棵树,当然前提是你能够看出来进栈的序列是先序遍历的结果,出栈的序列是中序遍历的结果。构建树的代码(递归):

Node CreateTree(int left, int right){    if(left > right) return NULL;    int root = preOrder[cur];    cur++;    int rootIndex = findRootIndex(root);    Node T = new TreeNode();    T->num = root;    if(left != right){        T->left = CreateTree(left,rootIndex-1);        T->right = CreateTree(rootIndex+1,right);    }    return T;}

 

 

Code:

#include
#include
#include
using namespace std;typedef struct Node *node;struct Node { int value; node leftSon; node rightSon; node father; Node():value(), leftSon(), rightSon(), father(){}};void postOrder(node h) { if (h != NULL) { postOrder(h->leftSon); postOrder(h->rightSon); cout << h->value << " "; }}int main() { int n; cin >> n; getchar(); string str, op, num; int pos; queue
q; stack
s; stack
leftOrRight; for (int i = 0; i < n*2; ++i) { getline(cin, str); if (str[1] == 'u') { pos = str.find(' '); op = str.substr(0, pos); num = str.substr(pos+1); q.push(op); q.push(num); } else { q.push(str); } } node head = new Node(); node prt = head; while (!q.empty()) { if (q.front() == "Push") { q.pop(); int value = stoi(q.front()); s.push(value); q.pop(); node temp = new Node(); if (prt->leftSon) { leftOrRight.push("right"); prt->rightSon = temp; temp->father = prt; } else { leftOrRight.push("left"); prt->leftSon = temp; temp->father = prt; } prt = temp; } else { q.pop(); if (leftOrRight.top() == "left") prt = prt->father; prt->value = s.top(); if (leftOrRight.top() == "right") prt = prt->father; s.pop(); leftOrRight.pop(); } } postOrder(prt); return 0;}

 

以上是我写的代码,因为没有注意到题目中暗含着两种遍历,所以写的代码也很乱,当然也是错误的。


以下的代码来自: 

#include 
#include
#include
#include
#include
#include
using namespace std; int N,cur;vector
preOrder;vector
inOrder; typedef struct TreeNode *Node;struct TreeNode{ int num; Node left,right; TreeNode(){ left = NULL; right = NULL; } }; int findRootIndex(int rootNum){ for(int i = 0;i < N; i++){ if(inOrder[i] == rootNum){ return i; } } return -1; } Node CreateTree(int left, int right){ if(left > right) return NULL; int root = preOrder[cur]; cur++; int rootIndex = findRootIndex(root); Node T = new TreeNode(); T->num = root; if(left != right){ T->left = CreateTree(left,rootIndex-1); T->right = CreateTree(rootIndex+1,right); } return T;} bool firstOutPut = true;void PostOrder(Node T){ if(!T) return; PostOrder(T->left); PostOrder(T->right); if(firstOutPut){ printf("%d",T->num); firstOutPut = false; }else{ printf(" %d",T->num); }} int main(){ stringstream ss; string Nstr; getline(cin,Nstr); ss << Nstr; ss >> N; ss.clear(); string input; stack
stk; int value; for(int i = 0; i < N * 2; i++){ getline(cin,input); if(input[1] == 'u'){ string num = input.substr(5); ss << num; ss >> value; ss.clear(); stk.push(value); preOrder.push_back(value); }else{ value = stk.top(); stk.pop(); inOrder.push_back(value); } } Node T = CreateTree(0,N-1); PostOrder(T); return 0;}

  

值得注意的是cur并没有赋初值,测试发现他的初值默认为0.

 

转载地址:http://ectuz.baihongyu.com/

你可能感兴趣的文章
Netty工作笔记0009---Channel基本介绍
查看>>
Netty工作笔记0011---Channel应用案例2
查看>>
Netty工作笔记0013---Channel应用案例4Copy图片
查看>>
Netty工作笔记0014---Buffer类型化和只读
查看>>
Netty工作笔记0020---Selectionkey在NIO体系
查看>>
Vue踩坑笔记 - 关于vue静态资源引入的问题
查看>>
Netty工作笔记0024---SelectionKey API
查看>>
Netty工作笔记0025---SocketChannel API
查看>>
Netty工作笔记0027---NIO 网络编程应用--群聊系统2--服务器编写2
查看>>
Netty工作笔记0028---NIO 网络编程应用--群聊系统3--客户端编写1
查看>>
Netty工作笔记0034---Netty架构设计--线程模型
查看>>
Netty工作笔记0050---Netty核心模块1
查看>>
Netty工作笔记0057---Netty群聊系统服务端
查看>>
Netty工作笔记0060---Tcp长连接和短连接_Http长连接和短连接_UDP长连接和短连接
查看>>
Netty工作笔记0063---WebSocket长连接开发2
查看>>
Netty工作笔记0070---Protobuf使用案例Codec使用
查看>>
Netty工作笔记0072---Protobuf内容小结
查看>>
Netty工作笔记0074---handler链调用机制实例1
查看>>
Netty工作笔记0077---handler链调用机制实例4
查看>>
Netty工作笔记0081---编解码器和处理器链梳理
查看>>