赞
踩
- #include <iostream>
- #include <stack>
- #include <queue>
-
- using namespace std;
-
- struct Node {
- char name;
- Node* child;
- Node* brother;
- Node(char str, Node* ch = nullptr, Node* br = nullptr) {
- name = str;
- child = ch;
- brother = br;
- }
- };
-
-
- int main()
- {
- string str;
- cin >> str;
-
- int mark = 0;
- stack<Node*> s;
- queue<Node*> q;
- Node* root;
-
- for (int i = 1; i < str.length() - 1; i++) {
- switch (str[i]) {
- case '(':
- mark = 1;
- break;
- case ')':
- s.pop();
- break;
- case ',':
- mark = 2;
- break;
- default:
- Node* p = new Node(str[i]);
- if (s.empty()) root = p;
- if (mark == 1)
- {
- Node* t = s.top();
- t->child = p;
- }
-
- if (mark == 2)
- {
- Node* t = s.top();
- t->brother = p;
- s.pop();
- }
- s.push(p);
- }
- }
- q.push(root);
-
- while (!q.empty()) {
- Node* t = q.front();
- cout << t->name;
- q.pop();
- if (t->child != nullptr) {
- t = t->child;
- while (t != nullptr) {
- q.push(t);
- t = t->brother;
- }
- }
- }
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。