创建二叉树求叶子节点个数
2016-07-21 11:23
281 查看
求二叉树叶子结点的个数,注意创建方法!
//求二叉树中叶子结点的个数 #include<iostream> #define N 63 using namespace std; char str[] = "ab#d##c#e##"; //先建立根节点,然后再左右结点建立 int i = -1; typedef struct node { struct node *leftChild; struct node *rightChild; char data; }BiTreeNode, *BiTree; //生成一个结点 BiTreeNode *createNode(int i) { BiTreeNode * q = new BiTreeNode; q->leftChild = NULL; q->rightChild = NULL; q->data = i; return q; } BiTree createBiTree1() { BiTreeNode *p = { NULL }; int i; for (i = 0; i < N; i++) p[i] = createNode(i + 1); // 把结点连接成树 for (i = 0; i < N / 2; i++) { p[i]->leftChild = p[i * 2 + 1]; p[i]->rightChild = p[i * 2 + 2]; } return p[0]; } void createBiTree2(BiTree &T) { i++; char c; if (str[i] && '#' == (c = str[i])) T = NULL; else { T = new BiTreeNode; T->data = c; createBiTree2(T->leftChild); createBiTree2(T->rightChild); } } int getLeafNode(BiTree T) { if (NULL == T) return 0; if (NULL == T->leftChild && NULL == T->rightChild) return 1; return getLeafNode(T->leftChild) + getLeafNode(T->rightChild); } int main() { BiTree T1; T1 = createBiTree1(); cout << getLeafNode(T1) << endl; BiTree T2; createBiTree2(T2); cout << getLeafNode(T2) << endl; return 0; }
相关文章推荐
- 需要掌握的es6特性(一)
- c#自定义集合类
- Agent简化云应用与企业内部部署应用之间的集成
- Java 打印各种直角三角形和乘法表
- 真机调试错误--Xcode cannot launch 项目名字 on iPhone because the device is locked
- ScrollView嵌套ListView只显示一行
- 杂乱的接头中恰逢一丝偶闲
- 学生学籍管理系统_管理员登陆对学生的信息进行操作
- Web Service学习总结
- 如何统计版本库中某一用户的提交次数
- (转)OpenCV中的常用函数
- 漂浮广告
- 第一章 MYSQL的架构和历史
- Java servlet
- CA证书的办理
- 函数
- !function()()和~function(){}意义,(function(){}())和(function(){})()区别
- HDU 2795 Billboard 线段树
- 自学目标
- rails常用的验证