1.从中序和后序构造二叉树
#include<iostream>
using namespace std;
struct node{
int data;
node* left;
node* right;
node():data(0),left(NULL),right(NULL){}
};
int inorder[7]={3,2,1,4,5,7,6};
int postorder[7]={3,1,2,5,6,7,4};
node* create_tree(int l1,int r1,int l2,int r2){
node *root=new node();
root->data=postorder[r2];
int mid=l1;
while(inorder[mid]!=postorder[r2]) mid++;
int cnt=mid-l1;
if(l1==mid){
root->left=NULL;
}
else{
root->left=create_tree(l1,mid-1,l2,l2+cnt-1);
}
if(r1==mid){
root->right=NULL;
}
else{
root->right=create_tree(mid+1,r1,l2+cnt,r2-1);
}
return root;
}
void visit(node *root){
if(root->left!=NULL){
visit(root->left);
}
if(root->right!=NULL){
visit(root->right);
}
if(root!=NULL){
cout<<root->data;
}
}
int main()
{
node *root=create_tree(0,6,0,6);
visit(root);
return 0;
}
2.从中序和前序构造二叉树
#include<iostream>
using namespace std;
struct node{
int data;
node* left;
node* right;
node():data(0),left(NULL),right(NULL){}
};
int inorder[7]={3,2,1,4,5,7,6};
int preorder[7]={4,2,3,1,7,5,6};
node* create_tree(int l1,int r1,int l2,int r2){
node *root=new node();
root->data=preorder[l2];
int mid=l1;
while(inorder[mid]!=preorder[l2]) mid++;
int cnt=mid-l1;
if(l1==mid){
root->left=NULL;
}
else{
root->left=create_tree(l1,mid-1,l2+1,l2+cnt);
}
if(r1==mid){
root->right=NULL;
}
else{
root->right=create_tree(mid+1,r1,l2+cnt+1,r2);
}
return root;
}
void visit(node *root){
if(root->left!=NULL){
visit(root->left);
}
if(root->right!=NULL){
visit(root->right);
}
if(root!=NULL){
cout<<root->data;
}
}
int main()
{
node *root=create_tree(0,6,0,6);
visit(root);
return 0;
}
3.例6-8
#include<iostream>
using namespace std;
struct node{
int data;
node* left;
node* right;
node():data(0),left(NULL),right(NULL){}
};
int inorder[7]={3,2,1,4,5,7,6};
int preorder[7]={4,2,3,1,7,5,6};
node* create_tree(int l1,int r1,int l2,int r2){
node *root=new node();
root->data=preorder[l2];
int mid=l1;
while(inorder[mid]!=preorder[l2]) mid++;
int cnt=mid-l1;
if(l1==mid){
root->left=NULL;
}
else{
root->left=create_tree(l1,mid-1,l2+1,l2+cnt);
}
if(r1==mid){
root->right=NULL;
}
else{
root->right=create_tree(mid+1,r1,l2+cnt+1,r2);
}
return root;
}
void visit(node *root){
if(root->left!=NULL){
visit(root->left);
}
if(root->right!=NULL){
visit(root->right);
}
if(root!=NULL){
cout<<root->data;
}
}
int best,best_sum;
//深度优先搜索天然具有从根节点到叶子节点搜索的能力
void dfs(node* n,int sum){
sum+=n->data;
if(n->left==NULL&&n->right==NULL){
if(sum<best_sum||(sum==best_sum&&n->data<best)){
best=n->data;
best_sum=sum;
}
}
if(n->left!=NULL)
dfs(n->left,sum);
if(n->right!=NULL)
dfs(n->right,sum);
}
int main()
{
node *root=create_tree(0,6,0,6);
visit(root);
best_sum=1000;
dfs(root,0);
cout<<endl<<best<<endl;
return 0;
}
4.最长路径
#include<iostream>
#include<queue>
using namespace std;
struct node{
int data;
node* left;
node* right;
node():data(0),left(NULL),right(NULL){}
};
int inorder[7]={3,2,1,4,5,7,6};
int preorder[7]={4,2,3,1,7,5,6};
node* create_tree(int l1,int r1,int l2,int r2){
node *root=new node();
root->data=preorder[l2];
int mid=l1;
while(inorder[mid]!=preorder[l2]) mid++;
int cnt=mid-l1;
if(l1==mid){
root->left=NULL;
}
else{
root->left=create_tree(l1,mid-1,l2+1,l2+cnt);
}
if(r1==mid){
root->right=NULL;
}
else{
root->right=create_tree(mid+1,r1,l2+cnt+1,r2);
}
return root;
}
void all_path(node* root,vector<int> path){
if(root!=NULL){
path.push_back(root->data);
if(root->left==NULL&&root->right==NULL){
for(vector<int>::size_type i=0;i<path.size();i++){
cout<<path[i]<<" ";
}
cout<<endl;
}
}
if(root->left!=NULL)
all_path(root->left,path);
if(root->right!=NULL)
all_path(root->right,path);
path.pop_back();
}
void longest(node* root,vector<int> path,int len,vector<int> &longest_path,int longest_len){
if(root!=NULL){
path.push_back(root->data);
len++;
if(root->left==NULL&&root->right==NULL){
if(len>longest_len){
longest_path.assign(path.begin(),path.end());
}
}
}
if(root->left!=NULL)
longest(root->left,path,len,longest_path,longest_len);
if(root->right!=NULL)
longest(root->right,path,len,longest_path,longest_len);
path.pop_back();
}
int main()
{
node *root=create_tree(0,6,0,6);
vector<int> path;
all_path(root,path);
cout<<"longest path:"<<endl;
vector<int> longest_path;
longest(root,path,0,longest_path,0);
for(vector<int>::size_type i=0;i<longest_path.size();i++){
cout<<longest_path[i]<<" ";
}
cout<<endl;
return 0;
}