二叉树节点
typedef class BinaryTree
{
public:
char ch;
BinaryTree *parent;
BinaryTree *LeftChild;
BinaryTree *RightChild;
} * BT;
空间复杂度为O(1)的中序遍历
BT last(BT root)
{
while (root->LeftChild != NULL)
{
root = root->LeftChild;
}
return root;
}
BT next(BT root)
{
if (root->RightChild != NULL)
return last(root->RightChild);
else
{
BT pa = root->parent;
while (root != pa->RightChild && pa != NULL)
{
root = pa;
pa = root->parent;
}
return pa;
}
}
void inOrder(BT root)
{
if (root == NULL)
return;
BT p = last(root);
while (p != NULL)
{
cout << p->ch << " ";
p = next(p);
}
}