构造二叉树

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;
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值