题解 | #二叉树遍历#
二叉树遍历
https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef
//指针用法、new用法 int *a=new int; *a=1;
#include <iostream>
using namespace std;
struct TreeNode{
char data;
TreeNode* leftchild;
TreeNode* rightchild;
TreeNode(char c): data(c), leftchild(NULL), rightchild(NULL){}
} ;
TreeNode* Build(int& position,string str)
{
char c=str[position++];
if(c=='#')return NULL;
TreeNode* root=new TreeNode(c);
root->leftchild=Build(position,str);
root->rightchild=Build(position,str);
return root;
}
void InOrder(TreeNode* root)
{
if(root==NULL)return ;
InOrder(root->leftchild);
cout<<root->data<<" ";
InOrder(root->rightchild);
}
int main() {
string str;
while (cin >>str) {
int position=0;
TreeNode* root=Build(position,str);
InOrder(root);
cout<<endl;
}
}

