二叉树打印路径

目录

已知二叉树的前序遍历和中序遍历序列,打印出这棵二叉树从根节点到叶子节点的所有路径;

已知二叉树的前序遍历和中序遍历序列,以及每个节点的权重,求出根节点到叶子结点权重最大的路径并输出。


已知二叉树的前序遍历和中序遍历序列,打印出这棵二叉树从根节点到叶子节点的所有路径;

//前序+中序建树,然后打印路径
#include<bits/stdc++.h>
using namespace std;

const int maxn=110;
typedef struct node{
    struct node *lchild;
    struct node *rchild;
    int val;
}node,*tree;
int pre[maxn],in[maxn];
vector<int>path[maxn],v;
int n,cur,cnt;

int finds(int x)
{
    for(int i=0;i<n;++i)
        if(in[i]==x)return i;
    return -1;
}
void build(tree &T,int left,int right)
{
    if(left>right || cur>=n)return;
    int root=pre[cur++];
    int index=finds(root);
    T=new node();
    T->val=root;
    if(left==right)T->lchild=T->rchild=NULL;
    else{
        build(T->lchild,left,index-1);
        build(T->rchild,index+1,right);
    }
}
void findPath(tree &T)
{
    if(T->lchild==NULL && T->rchild==NULL)
    {
        v.push_back(T->val);
        path[cnt++].assign(v.begin(),v.end());
        v.pop_back();
        return;
    }
    v.push_back(T->val);
    if(T->lchild!=NULL)findPath(T->lchild);
    if(T->rchild!=NULL)findPath(T->rchild);
    v.pop_back();
}
int main()
{
    tree T=NULL;
    scanf("%d",&n);
    for(int i=0;i<n;++i)scanf("%d",&pre[i]);
    for(int i=0;i<n;++i)scanf("%d",&in[i]);
    cur=0;
    build(T,0,n-1);
    cnt=0;
    findPath(T);
    for(int i=0;i<cnt;++i)
    {
        for(int j=0;j<path[i].size();++j)
            (j==path[i].size()-1)?printf("%d\n",path[i][j]):printf("%d=>",path[i][j]);
    }
}
/*
7
1 2 3 4 5 6 7
2 1 5 4 6 3 7
*/

已知二叉树的前序遍历和中序遍历序列,以及每个节点的权重,求出根节点到叶子结点权重最大的路径并输出。

//前序+中序建树,然后打印路径
#include<bits/stdc++.h>
using namespace std;

const int maxn=110;
typedef struct node{
    struct node *lchild;
    struct node *rchild;
    int val;
}node,*tree;
int pre[maxn],in[maxn];
int w[maxn];
vector<int>path[maxn],v;
int n,cur,cnt,sum,maxx;

int finds(int x)
{
    for(int i=0;i<n;++i)
        if(in[i]==x)return i;
    return -1;
}
void build(tree &T,int left,int right)
{
    if(left>right || cur>=n)return;
    int root=pre[cur++];
    int index=finds(root);
    T=new node();
    T->val=root;
    if(left==right)T->lchild=T->rchild=NULL;
    else{
        build(T->lchild,left,index-1);
        build(T->rchild,index+1,right);
    }
}
void findPath(tree &T)
{
    if(T->lchild==NULL && T->rchild==NULL)
    {
        v.push_back(T->val);
        sum+=T->val;
        if(sum>maxx)
        {
        	for(int i=0;i<cnt;++i)path[i].clear();
        	cnt=0;
        	path[cnt++].assign(v.begin(),v.end());
        	maxx=sum;
		}
		else if(sum==maxx)
        	path[cnt++].assign(v.begin(),v.end());
        v.pop_back();
        sum-=T->val;
        return;
    }
    v.push_back(T->val);sum+=T->val;
    if(T->lchild!=NULL)findPath(T->lchild);
    if(T->rchild!=NULL)findPath(T->rchild);
    v.pop_back();sum-=T->val;
}
int main()
{
    tree T=NULL;
    scanf("%d",&n);
    for(int i=0;i<n;++i)scanf("%d",&pre[i]);
    for(int i=0;i<n;++i)scanf("%d",&in[i]);
    for(int i=0;i<n;++i)scanf("%d",&w[i]);//权重
    cur=0;
    build(T,0,n-1);
    cnt=0;sum=0;maxx=-1;
    findPath(T);
    for(int i=0;i<cnt;++i)
    {
        for(int j=0;j<path[i].size();++j)
            (j==path[i].size()-1)?printf("%d\n",path[i][j]):printf("%d=>",path[i][j]);
    }
}
/*
7
1 2 3 4 5 6 7
2 1 5 4 6 3 7
1 2 3 4 5 6 7
*/

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/88884952