3)分别求二叉树的叶结点,度数为1的结点,度数为2的结点。
#include "stdafx.h"
#include<iostream>
using namespace std;
typedef struct BTreeNode
{
float data;
char optr;
struct BTreeNode *lchild,*rchild;
}BTree;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
int n0,n1,n2;//全局变量,用于统计结点数
void Count(BTree *t)
{
if(t)//前序遍历
{
if(t->lchild&&t->rchild)
{
n2++;
}
else if(t->lchild&&!t->rchild||!t->lchild&&t->rchild)
{
n1++;
}
else n0++;
if(t->lchild)Count(t->lchild);
if(t->rchild)Count(t->rchild);
}
}
4)求二叉树深度
#include "stdafx.h"
#include<iostream>
using namespace std;
typedef struct BTreeNode
{
float data;
char optr;
struct BTreeNode *lchild,*rchild;
}BTree;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
int Height(BTree *bt)
{
int hl,hr;
if(bt==NULL)
{
return 0;
}
else
{
hl=Height(bt->lchild);
hr=Height(bt->rchild);
if(hl>hr)return (hl+1);
else return (hr+1);
}
}