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

来源:互联网 发布:光环大数据培训 编辑:程序博客网 时间:2024/06/05 23:58

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

求二叉树的高度;

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




0 0
原创粉丝点击