PAT 1020. Tree Traversals (25)
2018-02-07 01:11
405 查看
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:
Sample Output:
其实思路挺好想的,利用中序序列和后序序列就能构造出二叉树,然后对其进行层次遍历。只不过我不知道为什么第三个测试点是测什么的,过不了。。如果有同学做过或者看出来了,非常欢迎您帮我提出来,在这里先道声感谢。
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
其实思路挺好想的,利用中序序列和后序序列就能构造出二叉树,然后对其进行层次遍历。只不过我不知道为什么第三个测试点是测什么的,过不了。。如果有同学做过或者看出来了,非常欢迎您帮我提出来,在这里先道声感谢。
#include <iostream> #include <string> #include <queue> using namespace std; struct TreeNode { int val; TreeNode* l_child; TreeNode* r_child; TreeNode(int v = 0,TreeNode* l = NULL,TreeNode* r = NULL):val(v),l_child(l),r_child(r){} }; typedef struct TreeNode* node; node createTree(string post,string in) { node root = NULL; if(post.size() == 0 || post.size() != in.size()) return root; int index = in.find(post[post.size()-1]); root = new struct TreeNode(post[post.size()-1] - '0',NULL,NULL); int l_len = index; int r_len = in.size() - 1 - index; string l_post = post.substr(0,l_len); string l_in = in.substr(0,l_len); string r_post = post.substr(l_len,r_len); string r_in = in.substr(index + 1,r_len); root->l_child = createTree(l_post,l_in); root->r_child = createTree(r_post,r_in); return root; } void levelTraversal(node root) { queue<node> q; if(root == NULL) return; q.push(root); bool tag = true; node p = root; while(!q.empty()) { int value = q.front()->val; if(tag) tag = false; else printf(" "); printf("%d",value); if(p->l_child) q.push(p->l_child); if(p->r_child) q.push(p->r_child); q.pop(); p = q.front(); } printf("\n"); } int main() { int n; cin>>n; string post; string in; for(int i = 0; i < n; i++) { int v; cin>>v; post += v + '0'; } for(int i = 0; i < n; i++) { int v; cin>>v; in += v + '0'; } node root = NULL; root = createTree(post,in); levelTraversal(root); return 0; }
相关文章推荐
- [PAT]1020. Tree Traversals (25)@Java
- PAT-A-1020. Tree Traversals (25)
- PAT 1020. Tree Traversals (25)
- PAT: 1020. Tree Traversals (25)
- (pat-a)1020. Tree Traversals (25)
- PAT (Advanced Level) Practise 1020. Tree Traversals (25)
- 浙大PAT (Advanced Level) Practise 1020 Tree Traversals (25)
- pat 1020. Tree Traversals (25)
- PAT甲级1020. Tree Traversals (25)
- PAT (Advanced Level) Practise 1020. Tree Traversals (25)
- PAT甲级 1020. Tree Traversals (25)
- 【PAT】1020. Tree Traversals (25)
- PAT 1020. Tree Traversals (25) 根据树的中序与后序,求层序
- 【PAT】1020. Tree Traversals (25)
- 浙大PAT 1020. Tree Traversals (25)
- PAT_A 1020. Tree Traversals (25)
- 【PAT】1020. Tree Traversals (25)
- PAT 1020. Tree Traversals (25)
- PAT (Advanced Level) 1020. Tree Traversals (25)
- PAT 1020. Tree Traversals (25)(树的构造与遍历,通过后序中序输出层次遍历)