题解 | #二叉树遍历#
二叉树遍历
http://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef
#include<iostream>
using namespace std;
typedef struct node {
char value;
struct node* left, * right;
}NodeTree, * Tree;
void BuildTree(Tree &root, string &str) {
if (str.size() && str.at(0) != '#') {
root = (Tree)malloc(sizeof(NodeTree));
root->value = str.at(0);
root->left = NULL;
root->right = NULL;
str.erase(str.begin());
BuildTree(root->left, str);
BuildTree(root->right, str);
}
else {
root = NULL;
str.erase(str.begin());
}
}
void Output(Tree root) {
if (root == NULL)
return;
Output(root->left);
cout << root->value << " ";
Output(root->right);
}
int main()
{
string str;
while (cin >> str) {
Tree root = NULL;
BuildTree(root, str);
Output(root);
}
}