随笔-11  评论-20  文章-0  trackbacks-0
方法一:
 1
 template <typename T>
 2 int countNodes(tnode<T> *t)
 3 {
 4     if (t->left == NULL && t->right == NULL)
 5         return 1;
 6     if (t->left == NULL)
 7         return 1 + countNodes(t->right);
 8     if (t->right == NULL)
 9         return countNodes(t->left) + 1;
10     
11     return countNodes(t->left) + countNodes(t->right) + 1;
12 }
方法二:
 1 template <typename T>
 2 int countNodes(tnode<T> *t)
 3 {
 4     int n = 0, leftValue, rightValue;
 5 
 6     if (t != NULL)
 7     {
 8         n++;
 9         leftValue = countNodes(t->left);
10        rightValue = countNodes(t->right);
11        return n + leftValue + rightValue;
12     }
13     else
14         return 0;
15 }

posted on 2009-08-28 08:27 diwayou 阅读(507) 评论(0)  编辑 收藏 引用 所属分类: 数据结构与算法

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理