Code
struct BinNode{
int val;
struct BinNode *left, *right;
BinNode(int x){
val = x;
left = right = NULL;
}
};
void creatTree(BinNode* &root){
root = new BinNode(1);
}
void preOrder(BinNode *root){
if(root){
cout << root->val << endl;
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(BinNode *root){
if(root){
inOrder(root->left);
cout << root->val << endl;
inOrder(root->right);
}
}
void postOrder(BinNode *root){
if(root){
postOrder(root->left);
postOrder(root->right);
cout << root->val << endl;
}
}
int getDepth(BinNode *root){
if(root){
int leftdepth = getDepth(root->left);
int rightdepth = getDepth(root->right);
return leftdepth > rightdepth ? leftdepth + 1 : rightdepth + 1;
}
else
return 0;
}
int leafCount(BinNode *root){
if(root){
if(root->left == NULL && root->right == NULL)
return 1;
else
return leafCount(root->left) + leafCount(root->right);
}
else
return 0;
}
int nodeCount(BinNode *root){
if(root)
return nodeCount(root->left) + nodeCount(root->right) + 1;
else
return 0;
}
BinNode *searchNode(BinNode *root, int val){
if(root == NULL)
return NULL;
if(root->val == val){
return root;
}
BinNode *p = searchNode(root->left, val);
return p ? p : searchNode(root->right, val);
}
bool insertNode(BinNode *root, int val, char LR, int x){
BinNode *p = searchNode(root, val);
if(p == NULL)
return false;
if(LR == 'L'){
if(p->left != NULL)
return false;
p->left = new BinNode(x);
}
else{
if(p->right != NULL)
return false;
p->right = new BinNode(x);
}
return true;
}
void clearTree(BinNode* &root){
if(root){
clearTree(root->left);
clearTree(root->right);
delete root;
root = NULL;
}
}