题目:https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef?tpId=40&tqId=21342&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking
主要是建树的代码还不太熟悉,说明自己这方面还有点薄弱
代码
#include <cstdio>#include <string>#include <iostream>using namespace std;string input;int pos;struct Node{ char name; Node * lchild; Node * rchild;}node;Node* create(){ char c = input[pos++]; if(c == '#'){ return NULL; } Node *root = new Node; root->name = c; root->lchild = create(); root->rchild = create(); return root;}void PreOrder(Node *root){ if(root == NULL) return; PreOrder(root->lchild); printf("%c ", root->name); PreOrder(root->rchild); return;}int main(){ while(getline(cin, input), input != ""){ pos = 0; Node *root = create(); PreOrder(root); printf("\n"); }}