This problem involves building and traversing binary trees.
Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes.
In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k+1.
For example, a level order traversal of the tree
is: 5, 4, 8, 11, 13, 4, 7, 2, 1.
In this problem a binary tree is specified by a sequence of pairs (n,s) where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of L's and R's where L indicates a left branch and R indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once.
InputThe input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs (n,s) as described above separated by whitespace. The last entry in each tree is (). No whitespace appears between left and right parentheses.
All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file.
OutputFor each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string ``not complete'' should be printedSample Input
(11,LL) (7,LLL) (8,R) (5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) () (3,L) (4,R) ()
Sample Output
5 4 8 11 13 4 7 2 1 not complete
给定一些二叉树的序列, 你需要输出每颗二叉树的层序遍历序列。每棵树的节点数不超过256。
层序遍历由上到下,相同层由左到右输出。
例如:
层序遍历的结果为: 5, 4, 8, 11, 13, 4, 7, 2, 1.
(n,s) n 是节点的值,s为从根节点出发到该点的路径。s由'L'和‘R’组成,L表示左分支,R表示右分支。上图中, 节点 13 就表示为 (13,RL), 节点 2 的表示为 (2,LLR). 根节点 5 表示为 (5,) ,根节点路径为空。 一颗二叉树被认为是"完整的"当每个从根节点出发的路径都只对应一个值时。
Input输入包含多组数据. 每棵树给定一系列对 (n,s) 。结束标记为 (). 每对括号之间没有空格。
每个节点包含一个正整数。输入的每棵树至少包含一个节点不超过256个节点. EOF表示所有输入结束。
Output对于每个“完整的”树, 输出他的层序遍历结果。如果一棵树不是“完整的”,也就是说, 树里的某个节点没有给定值或者给定某个节点的值超过了一次 , 那么就输出 ``not complete'' 。
//#include <bits/stdc++.h> #include <iostream> #include <cstdio> #include <cmath> #include<cstring> #include <algorithm> #include <queue> #include<map> using namespace std; typedef long long ll; const ll inf = 1e13; const int mod = 1000000007; const int mx = 256+5; //check the limits, dummy typedef pair<int, int> pa; const double PI = acos(-1); ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } #define swa(a,b) a^=b^=a^=b #define re(i,a,b) for(int i=(a),_=(b);i<_;i++) #define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--) #define clr(a) memset(a, 0, sizeof(a)) #define lowbit(x) ((x)&(x-1)) #define mkp make_pai //void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); } int n, m, k; struct node { bool isAssign; int val; node* left; node* right; node() :isAssign(0), left(NULL), right(NULL) {} }; node* root; bool failed; node* newnode() { return new node; } void removeTree(node* p) { if (p == NULL)return; removeTree(p->left); removeTree(p->right); delete p; } void insertNode(int val, char* seq) { int n = strlen(seq); node* p = root; re(i, 0, n) { if (seq[i] == 'L') { if (p->left == NULL) { p->left = newnode(); } p = p->left; } else if (seq[i] == 'R') { if (p->right == NULL) { p->right = newnode(); } p = p->right; } } if (p->isAssign) { failed = 1; } p->val = val; p->isAssign = 1; } char s[mx]; bool read() { removeTree(root); failed = 0; root = newnode(); while (1) { if (scanf("%s", s) != 1) { return 0; } if (!strcmp(s, "()")) { break; } int val; sscanf(&s[1], "%d", &val); insertNode(val, strchr(s, ',') + 1); } return 1; } bool bfs(vector<int>& ans) { queue<node*>q;// ans.clear(); q.push(root); while (!q.empty()) { node* p = q.front(); q.pop(); if (!p->isAssign) { return 0; } ans.push_back(p->val); if (p->left != NULL) { q.push(p->left); } if (p->right != NULL) { q.push(p->right); } } return 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); vector<int>ans; while (read()) { if (!bfs(ans)) { failed = 1; } if (failed) { cout << "not complete" << endl; } else { re(i, 0, ans.size()) { if (i != 0) { cout << ' '; } cout << ans[i]; } cout << endl; } } return 0; }