//显示树中所有节点的值
void show(tnode<int> *root)
{
if (root)
{
cout<< root->value<<"/n";
show(root->left);
show(root->right);
}
}
//输入树的每层节点值
void GetLayer(tnode<int>*root,vector<int>& v)
{
if (root == NULL)
return ;
queue< tnode<int>* > q;
tnode<int> *p;
q.push(root);
while( !q.empty())
{
p = q.front();
v.push_back(p->value);
q.pop();
if (p->left != NULL)
q.push(p->left);
if (p->right != NULL)
q.push(p->right);
}
}
//计算树的叶节点个数
void CountLeaf(tnode<int>*root, int &nCount)
{
if ( NULL == root)
return;
if ( (NULL ==root->left ) && (NULL == root->right) )
++nCount;
CountLeaf(root->left, nCount);
CountLeaf(root->right,nCount);
}
//计算树深度
int CoutDepth(tnode<int>*root)
{
int depthLeft = 0, depthRight = 0, depthVal = 0;
if ( NULL == root)
depthVal = - 1;
else
{
depthLeft = CoutDepth(root->left);
depthRight= CoutDepth(root->right);
depthVal = 1 + (depthLeft > depthRight ? depthLeft : depthRight);
}
return depthVal;
}
//树节点的个数
void nodeCout(tnode<int>*root, int &n)
{
if (NULL == root)
return;
if (root)
{
++n;
nodeCout(root->left,n);
nodeCout(root->right,n);
}
}
//复制树
tnode<int>* Copy(tnode<int>*root)
{
tnode<int>*left, *right, *newNode;
if (NULL == root)
return NULL;
left = Copy(root->left);
right = Copy(root->right);
newNode = new tnode<int>(root->value,left,right);
return newNode;
}