typedef char E;
struct TreeNode {
E element;
struct TreeNode *left;
struct TreeNode *right;
int leftTag, rightTag;
};
typedef struct TreeNode *Node;
Node createNode(E element) {
Node node = malloc(sizeof(struct TreeNode));
if (node == NULL) return NULL;
node->left = node->right = NULL;
node->leftTag = node->rightTag = 0;
node->element = element;
return node;
}
Node pre = NULL;
void inOrder(Node root) {
if (root == NULL) return;
if (root->leftTag == 0)
inOrder(root->left);
if (root->left == NULL) {
root->left = pre;
root->leftTag = 1;
}
if (pre && pre->right == NULL) {
pre->right = root;
pre->rightTag = 1;
}
pre = root;
if (root->rightTag == 0)
inOrder(root->right);
}
void f(Node TwoTree) {
while (TwoTree) {
while (TwoTree && TwoTree->leftTag == 0)
TwoTree = TwoTree->left;
printf("%c", TwoTree->element);
while (TwoTree && TwoTree->rightTag == 1) {
TwoTree = TwoTree->right;
printf("%c", TwoTree->element);
}
TwoTree = TwoTree->right;
}
}
int main() {
Node a = createNode('A');
Node b = createNode('B');
Node c = createNode('C');
Node d = createNode('D');
Node e = createNode('E');
a->left = b;
b->left = d;
a->right = c;
b->right = e;
inOrder(a);
f(a);
}