题干
C++实现
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
using namespace std;
struct TreeNode
{
char data;
TreeNode* left;
TreeNode* right;
};
TreeNode* RecursiveBuildTree(int& i, char str[]) {
char c = str[i];
++i;
if (c == '#') {
return NULL;
}
else {
TreeNode* pNew = new TreeNode();//堆中申请的变量,可以作为函数返回值
pNew->data = c;
pNew->left = RecursiveBuildTree(i, str);
pNew->right = RecursiveBuildTree(i, str);
return pNew;
}
}
void InOrder(TreeNode* proot) {
if (proot == NULL) {
return;
}
InOrder(proot->left);
printf("%c ", proot->data);
InOrder(proot->right);
}
int main() {
char str[1000];
while (scanf("%s", str) != EOF) {
int i = 0;
TreeNode* proot = RecursiveBuildTree(i, str);
InOrder(proot);
printf("\n");
}
return 0;
}