https://blog.csdn.net/ac_blood/article/details/77047877
#include <iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef int TelemType;
typedef struct BinaryTreeNode{
TelemType data;
struct BinaryTreeNode *Left;
struct BinaryTreeNode *Right;
}Node;
Node* createBinaryTree(){
Node *p;
TelemType ch;
cin>>ch;
if(ch==0){
p=NULL;
}else {
p=(Node*)malloc(sizeof(Node));
p->data =ch;
p->Left = createBinaryTree();
p->Right = createBinaryTree();
}
return p;
}
void preOrder(Node *root){
if(root){
cout<<root->data<<" ";
preOrder(root->Left);
preOrder(root->Right);
}
}
void inOrder(Node *root){
if(root){
inOrder(root->Left);
cout<<root->data<<" ";
inOrder(root->Right);
}
}
void lastOrder(Node* root){
if(root){
lastOrder(root->Left);
lastOrder(root->Right);
cout<<root->data<<" ";
}
}
int Nodenum(Node* root)
{
if(root == NULL)
{
return 0;
}
else
{
return 1+Nodenum(root->Left)+Nodenum(root->Right);
}
}
int Depthoftree(Node* root){
if(root){
return Depthoftree(root->Left)>Depthoftree(root->Right)?Depthoftree(root->Left)+1:
Depthoftree(root->Right)+1;
}else{
return 0;
}
}
int Leafnum(Node *root){
if(!root){
return 0;
}else if((root->Left==NULL)&&(root->Right==NULL)) {
return 1;
}else {
return Leafnum(root->Left)+Leafnum(root->Right);
}
}
int main(){
Node *root=NULL;
root=createBinaryTree();
cout<<"success"<<endl;
cout<<"总节点数:"<<Nodenum(root)<<endl;
cout<<"二叉树的深度:"<<Depthoftree(root)<<endl;
cout<< "叶子节点数:"<<Leafnum(root)<<endl;
cout<<"前序遍历:"<<endl;
preOrder(root);
puts(" ");
cout<<"中序遍历:"<<endl;
inOrder(root);
puts(" ");
cout<<"后序遍历:"<<endl;
lastOrder(root);
puts(" ");
}
示范
6 2 1 0 0 4 3 0 0 0 8 0 0
success
总节点数:6
二叉树的深度:4
叶子节点数:3
前序遍历:
6 2 1 4 3 8
中序遍历:
1 2 3 4 6 8
后序遍历:
1 3 4 2 8 6
Process exited after 20.5 seconds with return value 0
请按任意键继续. . .
草率的码
#include<bits/stdc++.h>
using namespace std;
typedef struct Tree{
int data;
Tree*left;
Tree*right;
}Node;
Node* buildtree(){
int ch;
Node* p;
cin>>ch;
if(ch==0){
p=NULL;
}else {
p=(Node*)malloc(sizeof(Node));
p->data =ch;
p->left=buildtree();
p->right=buildtree();
}
return p;
}
int Nodenum(Node *root){
if(root==NULL) return 0;
else return 1+Nodenum(root->left)+Nodenum(root->right);
}
int leafnum(Node *root){
if(root==NULL) return 0;
else if((root->left==NULL)&&(root->right==NULL)) return 1;
else return leafnum(root->left)+leafnum(root->right);
}
int depth(Node *root){
if(root==NULL) return 0;
else return depth(root->left )>depth(root->right)?depth(root->left )+1:
depth(root->right)+1;
}
void pre(Node *root){
if(root){
cout<<root->data<<" ";
pre(root->left);
pre(root->right);
}
}
void mid(Node *root){
if(root){
mid(root->left);
cout<<root->data<<" ";
mid(root->right);
}
}
void last(Node *root){
if(root){
last(root->left);
last(root->right);
cout<<root->data<<" ";
}
}
int main(){
Node *root;
root=buildtree();
cout<<"所有节点的数目"<<Nodenum(root)<<endl;
cout<<"叶子节点"<<leafnum(root)<<endl;
cout<<"深度"<<depth(root)<<endl;
cout<<"前序"<<endl;
pre(root);
cout<<endl;
cout<<"中序"<<endl;
mid(root);
cout<<endl;
cout<<"后序"<<endl;
last(root);
cout<<endl;
}
#
typedef struct Tree{
char data;
Tree*left;
Tree*right;
}Node;
Node* buildtree(){
char ch;
Node* p;
cin>>ch;
if(ch=='#'){
p=NULL;
}else {
p=(Node*)malloc(sizeof(Node));
p->data =ch;
p->left=buildtree();
p->right=buildtree();
}
return p;
}