1.题目要求按照后续和中序还原二叉树,然后进行层次遍历输出。
还原的函数为:
class TreeNode{ public: int val; TreeNode* l, *r; TreeNode(int x) :val(x), l(NULL), r(NULL){}; TreeNode() :val(0), l(NULL), r(NULL){}; }; TreeNode* build(vector<int>&postOrder, int postStart, int postEnd, vector<int>&inOrder, int inStart, int inEnd, int&index, vector<TreeNode>&tree) { if (postStart == postEnd) {//如果start和end相同,即这个是叶子节点 tree[index++] = TreeNode(postOrder[postEnd]); return &tree[index - 1]; } else if (postStart > postEnd) return NULL; else { int rootVal = postOrder[postEnd];//后序遍历的末位是根 int rootPos = inStart;//根在中序遍历中的位置 for (; rootPos <= inEnd; rootPos++) { if (rootVal == inOrder[rootPos]) break; } int returnNode = index++; tree[returnNode] = TreeNode(rootVal); //注意newPostEnd = postStart + rootPos - inStart-1 tree[returnNode].l = build(postOrder, postStart, postStart + rootPos - inStart-1, inOrder, inStart, rootPos - 1, index, tree); tree[returnNode].r = build(postOrder, postStart + rootPos - inStart , postEnd - 1, inOrder, rootPos + 1, inEnd, index, tree); return &tree[returnNode]; } }
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
7 2 3 1 5 7 6 4 1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
AC代码:
//#include<string> //#include <iomanip> #include<vector> #include <algorithm> //#include<stack> #include<set> #include<queue> #include<map> //#include<unordered_set> //#include<unordered_map> //#include <sstream> //#include "func.h" //#include <list> #include<stdio.h> #include<iostream> #include<string> #include<memory.h> #include<limits.h> using namespace std; class TreeNode{ public: int val; TreeNode* l, *r; TreeNode(int x) :val(x), l(NULL), r(NULL){}; TreeNode() :val(0), l(NULL), r(NULL){}; }; TreeNode* build(vector<int>&postOrder, int postStart, int postEnd, vector<int>&inOrder, int inStart, int inEnd, int&index, vector<TreeNode>&tree) { if (postStart == postEnd) {//如果start和end相同,即这个是叶子节点 tree[index++] = TreeNode(postOrder[postEnd]); return &tree[index - 1]; } else if (postStart > postEnd) return NULL; else { int rootVal = postOrder[postEnd];//后序遍历的末位是根 int rootPos = inStart;//根在中序遍历中的位置 for (; rootPos <= inEnd; rootPos++) { if (rootVal == inOrder[rootPos]) break; } int returnNode = index++; tree[returnNode] = TreeNode(rootVal); //注意newPostEnd = postStart + rootPos - inStart-1 tree[returnNode].l = build(postOrder, postStart, postStart + rootPos - inStart-1, inOrder, inStart, rootPos - 1, index, tree); tree[returnNode].r = build(postOrder, postStart + rootPos - inStart , postEnd - 1, inOrder, rootPos + 1, inEnd, index, tree); return &tree[returnNode]; } } int main(void) { int nodeNum; cin >> nodeNum; vector<int> postOrder(nodeNum, 0);//后序 vector<int> inOrder(nodeNum, 0);//中序 for (int i = 0; i < nodeNum; i++) { cin >> postOrder[i]; } for (int i = 0; i < nodeNum; i++) { cin >> inOrder[i]; } vector<TreeNode> tree(nodeNum);//提前分配后内存空间,方便后面的连接 int index=0; TreeNode*ans = build(postOrder, 0, nodeNum - 1, inOrder, 0, nodeNum - 1, index, tree); int total = 0; int total2 = 0; queue<TreeNode*> q; if (ans != NULL) { total = 1; q.push(ans); } vector<int> outNum(0); while (!q.empty()) {//进行BFS while (total--) { TreeNode* front = q.front(); q.pop(); outNum.push_back(front->val); if (front->l!=NULL) { q.push(front->l); total2++; } if (front->r != NULL) { q.push(front->r); total2++; } } total = total2; total2 = 0; } for (int i = 0; i < outNum.size(); i++) { cout << outNum[i]; if (i != outNum.size() - 1) cout << " "; } return 0; }