您的位置:首页 > 其它

二叉树的一些简单算法(二)

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);
}
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: