二叉树的一些简单算法(二)
2014-11-01 23:07
363 查看
二叉树的一些简单算法(二)
求二叉树的高度;
int GetHight(BTree * root){ //得到高度
int l1, l2, l = 0;
if (root){
l1 = GetHight(root->left) + 1;
l2 = GetHight(root->right) + 1;
l = l1 > l2 ? l1 : l2;
}
return l;
}
求二叉树的节点个数;
int GetNum(BTree * root){ //输出有多少节点
static int length = 0;
if (root){
length++;
GetHight(root->left);
GetHight(root->right);
}
return length;
}
输出二叉树的叶子节点;
void PreLeaf(BTree * root){ //输出叶子节点
if (root){
if (!root->left && !root->right){
cout << root->data;
}
PreLeaf(root->left);
PreLeaf(root->right);
}
}
输出二叉树的双分支节点;
BTree * PrintBLeaf(BTree * root){
if (root){
if (root->left && root->right){
cout << root->data;
}
PrintBLeaf(root->left);
PrintBLeaf(root->right);
}
}
求二叉树的高度;
int GetHight(BTree * root){ //得到高度
int l1, l2, l = 0;
if (root){
l1 = GetHight(root->left) + 1;
l2 = GetHight(root->right) + 1;
l = l1 > l2 ? l1 : l2;
}
return l;
}
求二叉树的节点个数;
int GetNum(BTree * root){ //输出有多少节点
static int length = 0;
if (root){
length++;
GetHight(root->left);
GetHight(root->right);
}
return length;
}
输出二叉树的叶子节点;
void PreLeaf(BTree * root){ //输出叶子节点
if (root){
if (!root->left && !root->right){
cout << root->data;
}
PreLeaf(root->left);
PreLeaf(root->right);
}
}
输出二叉树的双分支节点;
BTree * PrintBLeaf(BTree * root){
if (root){
if (root->left && root->right){
cout << root->data;
}
PrintBLeaf(root->left);
PrintBLeaf(root->right);
}
}
相关文章推荐
- 二叉树的一些简单算法(一)
- C# 一些简单算法
- 【题目9】一些简单的查找交换等算法
- 二叉树11个简单算法
- 一些简单的lua面试题,小算法,算不上算法的算法。。╮(╯▽╰)╭博主面试的辛酸旅程
- 二叉树的一些算法
- 二叉树的一些简单操作
- python 一些简单的算法
- 一些简单的链表算法二
- 二叉树的一些通用算法(C)
- 【题目19】一些简单的算法练习题(1)
- 二叉树的一些基础算法
- 二叉树的一些基础算法
- 关于二叉树的一些算法
- 二叉树的几种简单算法
- 以下是关于二叉树操作的11个简单算法
- 一些简单但是常见的算法题目(笔记类)
- 一些简单常用算法整理学习 转
- 四舍五入的一些简单写法(利用内置函数,算法2种写法)
- 自动化脚本中运到的一些简单算法