先序创建
说明:空树用字符‘0’表示
class TreeNode {
public:
char data;
ListNode *lChild;
ListNode *rChild;
ListNode() {
lChild = NULL;
rChild = NULL;
}
};
class Tree {
public:
TreeNode *root;
int pos;
Tree() {
pos = 0;
}
TreeNode *createTree(char str[]) {
char data = str[pos];
pos++;
if(data != '0') {
ListNode *p = new TreeNode();
p->data = data;
p->lChild = createTree(str);
p->rChild = createTree(str);
return p;
} else {
return NULL;
}
}
};