树中节点结构为:
typedef struct TreeNode
{
int data;
TreeNode * left;
TreeNode * right;
TreeNode * parent;
}TreeNode;
void pre_order(TreeNode * Node)
{
if(Node != NULL)
{
printf("%d ", Node->data);
pre_order(Node->left);
pre_order(Node->right);
}
}
调用时: pre_order(Root); //Root为树的根
核心代码:
procedure first(i:longint);
begin
write(a);
if a[i*2]<>0 then first(i*2);
if a[i*2+1]<>0 then first(i*2+1);
end;
二叉树定义
递归实现
非递归实现