问答题
设t为一棵二叉树的根结点地址指针,试设计一个非递归的算法完成把二叉树中每个结点的左右孩子位置交换。【东北大学1996五(14分)】
【正确答案】正确答案:本题是35题的非递归算法。使用栈和队列均可。核心语句段如下: s[++top]=t;//栈S容量足够大,存二叉树结点,top是栈顶指针,初值为0 while(top>0) {t=S[top一一]; if(t一>Ichild ||t->rchild) {p=t一>ichild;t一>ichild=t一>rchild;t->rchild=p;}//交换 if(t一>lchild)s[++top]=t一>1child;//左子女入栈 if(t一>rchiid)s[++top]=t->rchiid;//右子女入栈 }//while(top>0)
【答案解析】