6.41

来源:互联网 发布:2k16mc科比捏脸数据 编辑:程序博客网 时间:2024/06/18 10:39
6.41③ 编写递归算法,在二叉树中求位于先序序列中
第k个位置的结点的值。

要求实现下列函数:
TElemType PreOrder(BiTree bt, int k);
/* bt is the root node of a binary linked list, */
/* Preorder travel it and find the node whose   */
/* position number is k, and return its value.  */
/* if can't found it, then return '#'.          */

二叉链表类型定义:
typedef struct BiTNode {
    TElemType data;
    BiTNode  *lchild, *rchild;

} BiTNode, *BiTree;

************************************************************************************************************


TElemType PreCount(BiTree bt,int k,int &i)

{
  if(!bt)return '#';
  TElemType e;
   if(i==k)return bt->data;
   i++;
   e=PreCount(bt->lchild,k,i);
   if(e=='#')
    return PreCount(bt->rchild,k,i);
   else return e;
}
TElemType PreOrder(BiTree bt, int k)
/* bt is the root node of a binary linked list, */
/* Preorder travel it and find the node whose   */
/* position number is k, and return its value.  */
/* if can't found it, then return '#'.          */
{
  int i=1;
  return PreCount(bt,k,i);
}



原创粉丝点击