二叉树算法笔记

二叉树的链式存储定义

typedef struct BiTNode {
      int data;
      struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

统计叶子结点个数

int LeavesCount(BiTNode T) {
      int count;
      if(bt == NULL)
            return count = 0;
      else if(T->lchild == NULL && T->rchild == NULL)
            return count = 1;
      else 
            return LeavesCount(T->lchild) + LeavesCount(T->rchild);
}

二叉树的层次遍历

void LevelOrder(BiTree T){
      InitQueue(Q);
      BiTree p = T;
      EnQueue(Q, T);      //根结点入队
      while(!IsEmpty(Q)){
            DeQueue(Q, p);
            visit(p);
            if(p->lchild != NULL)
                  EnQueue(Q, p->lchild);
            if(p->rchild != NULL)
                  EnQueue(Q, p->rchild);
      }
}
posted @ 2020-08-10 18:10  0202zc  阅读(141)  评论(0编辑  收藏  举报