首页 > 试题广场 >

下面是二叉树的先序遍历的非递归算法,则(1)、(2)、(3)

[单选题]
下面是二叉树的先序遍历的非递归算法,则(1)、(2)、(3)、(4)处分别是()
void Preorder(BTNode *bt)
{
    BTNode *s[MaxSize],*p;//顺序栈s中保存的是结点指针
    int top=-1;
    if(bt!=NULL)
    {
        top++;//根结点进栈
        s[top]=bt;
        while(top>-1)//栈不空时循环
        {
            p=s[top];//退栈并访问该节点
            top--;
            printf("%c",p->data);
            if((1))
            {
                top++;
                (2);
            }
            if((3))
            {
                top++;
                (4);
            }
        }
    }
}
  • p->rchild!=NULL、s[top]=p->rchild、p->lchild!=NULL、s[top]=p->lchild
  • p->lchild!=NULL、s[top]=p->lchild、p->rchild!=NULL、s[top]=p->rchild
  • p!=NULL、s[top]=p->rchild、p!=NULL、s[top]=p->lchild
  • p!=NULL、s[top]=p->lchild、p!=NULL、s[top]=p->rchild
不要忘了栈的FILO,所以右子树先进栈
发表于 2025-12-23 22:02:46 回复(0)