#include<iostream>
#include <algorithm>
using namespace std;
typedef struct treenode
{
int val;
struct treenode* left;
struct treenode* right;
}*tree;
void insert(tree* node, int val)
{
treenode* temp = NULL;
if (!(*node))
{
temp = (treenode*)malloc(sizeof(treenode));
temp->val = val;
temp->left = NULL;
temp->right = NULL;
*node = temp;
return;
}
if (val < ((*node)->val))
{
insert(&(*node)->left, val);
}
else if (val > ((*node)->val))
{
insert(&(*node)->right, val);
}
}//插入操作
void through(treenode* node)
{
if (!node)
return;
cout << node->val;
through(node->left);
through(node->right);
}//前序遍历
void throughl(treenode* node)
{
if (!node)
return;
throughl(node->left);
cout << node->val;
throughl(node->right);
}//中序遍历
void througha(treenode* node)
{
if (!node)
return;
througha(node->left);
througha(node->right);
cout << node->val;
}//后序遍历
int deeper(treenode* a)
{
if (a == NULL)
return 0;
int m = deeper(a->left);
int n = deeper(a->right);
if (m > n)
return(m + 1);
else
return(n + 1);
}//统计深度
int sumleaf(treenode* a)
{
if (a == NULL)
return 0;
else
return sumleaf(a->left) + sumleaf(a->right) + 1;
}//统计节点
void deltree(treenode* node)
{
if (node)
{
deltree(node->left);
deltree(node->right);
free(node);
}
}//删除操作
int main()
{
treenode* a = NULL;
int b[5] = { 3,2,1,4,5 };
for (int i = 0; i < 5; i++)
insert(&a, b[i]);
through(a);
cout << endl;
throughl(a);
cout << endl;
througha(a);
int w = deeper(a);
int t = sumleaf(a);
cout << w<<endl;
cout << t << endl;
deltree(a);
}