0. 代码实现
#include<iostream>
using namespace std;
typedef struct BiTNode {
int data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
int f(BiTree tree) {
if (tree) {
return f(tree->lchild) + f(tree->rchild) + 1;
}
return 0;
}
int main() {
BiTree tree = new BiTNode;
BiTNode *node1 = new BiTNode, *node2 = new BiTNode, *node3 = new BiTNode, *node4 = new BiTNode, *node5 = new BiTNode;
tree->data = node1->data = node2->data = node3->data = node4->data = node5->data = 1;
tree->lchild = node1; tree->rchild = node2;
node1->lchild = NULL; node1->rchild = node3;
node2->lchild = node4; node2->rchild = NULL;
node3->lchild = NULL; node3->rchild = node5;
node4->lchild = NULL; node4->rchild = NULL;
node5->lchild = NULL; node5->rchild = NULL;
cout << f(tree);
return 0;
}
1. 运行截图