水题,直接构造就行。因为是一棵完全二叉树
#include<iostream> #include<string> using namespace std; struct Node{ Node *left; Node *right; char val; Node(char x):val(x),left(NULL),right(NULL){} }; string s; Node* creatTree(int &i){ if(s[i]=='#')return NULL; Node *node=new Node(s[i]); node->left=creatTree(++i); node->right=creatTree(++i); return node; } void inOrder(Node *root){ if(root==NULL)return ; inOrder(root->left); cout<<root->val<<" "; inOrder(root->right); } int main(){ while(cin>>s){ int i=0; Node *root=creatTree(i); inOrder(root); cout<<endl; } return 0; }