方法一:
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 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 }
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 }