首页 > 技术文章 > 算法(7) —— 统计树的特定点

hanxinle 2017-09-20 17:25 原文

1. 统计节点数

1 int CountNodes ( SearchTree T )     {
2     if ( T == NULL)
3         return 0;
4     else 
5         return 1 + CountNodes ( T->Left) + CountNodes (T->Right);
6 }
int CountNodes ( SearchTree T )

2.统计叶子树

1 int CountLeaves (SearchTree T)      {
2     if  (T == NULL)
3         return 0;
4     else if (T->Left == NULL && T->Right == NULL)
5         return 1;
6     else return CountLeaves (T->Right) + CountLeaves (T -> Left);
7 }
int CountLeaves (SearchTree T )

3.统计左右孩子都存在的双亲节点数

int CountFull  (SearchTree T)    {
    if ( T == NULL)   
        return 0;
    else return (T->Right != NULL && T->Left != NULL) +
                 CountFull (T->Left) + CountFull (T->Right);
}
int CountFull (SearchTree T)

 

 

以上函数已经更新到 tree.h 中。

推荐阅读