如何创建一个二叉树?有人想的出来嘛?

输入的第一行为根节点;第二行以后每行的第二元为第一元的左节点,第三元为第一元的右节点,0表示空;其中#为输入结束标记。

img

```cpp #include using namespace std; typedef struct node { char val; struct node *left; struct node *right; } node; #define ALLOC_(name, val) \ node *name = malloc(sizeof node); \ name->val = val; \ name->left = name->right = null node *buildTree() { char a, b, c; cin >> a; if (a == '#') return null; unordered_map mp; ALLOC_(root, a); mp[root->val] = root; while (cin >> a) { if (a == '#') break; cin >> b>> c; auto parent = mp.find(a); auto left = mp.find(b); auto right = mp.find(c); if (left == mp.end()) { ALLOC_(p, b); mp[p->val] = p; left= mp.find(p->val); } if (right == mp.end()) { ALLOC_(p, b); mp[p->val] = P; right= mp.find(p->val); } parent->second->left = left->second; parent->second->right - right->second; } return root; } ```