- 求二叉树的先序序列
- 2021-01-20 17:19:35 @
这是啥WA... 评测机在自我否定吗 :sweat_smile:
1 条评论
-
w568w LV 8 @ 2021-01-20 17:32:08
代码丢这儿了...
#include <bits/stdc++.h> using namespace std; struct node { char v = 0; node *l = nullptr, *r = nullptr; }; node *in_nodes[10]; node *pos_nodes[10]; node *buildTree(int inStart, int inEnd, int posStart, int posEnd) { if (inStart >= inEnd || posStart >= posEnd)return nullptr; node *root = pos_nodes[posEnd - 1]; int index = inStart; while (in_nodes[index]->v != root->v)index++; root->l = buildTree(inStart, index, posStart, posStart + index - inStart); root->r = buildTree(index + 1, inEnd, posStart + index - inStart, posEnd - 1); return root; } void prePrint(node *root) { if (root == nullptr)return; cout << root->v; prePrint(root->l); prePrint(root->r); } int main() { char c; int len = 0; node *tmp_nodes[256]; while ((c = getchar()) != '\n') { node *tmp = new node(); tmp->v = c; in_nodes[len++] = tmp_nodes[c] = tmp; } len = 0; while ((c = getchar()) != '\n') { pos_nodes[len++] = tmp_nodes[c]; } prePrint(buildTree(0, len, 0, len)); return 0; }
把
cout << root->v;
改成putchar(root->v);
就好了。是评测机的锅嘛?
- 1