解法:
#include<iostream>
using namespace std;
struct TreeNode {char val;TreeNode* left;TreeNode* right;TreeNode(char c) :val(c), left(NULL), right(NULL) {};
};
TreeNode* buildTree() {char c;cin >> c;if (c == '#') {return NULL;}TreeNode* root = new TreeNode(c);root->left = buildTree();root->right = buildTree();return root;
}
void dfs(TreeNode* root) {if (root == NULL) return;dfs(root->left);dfs(root->right);cout << root->val;
}
int main() {TreeNode* root = buildTree();dfs(root);return 0;
}