UVA 548(二叉树重建与遍历)

J - Tree

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Submit Status

Appoint description: 
System Crawler  (2014-05-16)

Description

 Tree 

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values
of nodes along that path.

Input

The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated
with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have
more than 10000 nodes or less than 1 node.

Output

For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.

Sample Input

3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255

Sample Output

1
3
255

Miguel A. Revilla

1999-01-11

题意:给你二叉树的中序与后序,求从根到叶子所有值之和最小的叶子的值。

根据中序和后序递归建树,直接遍历即可。

#include<stdio.h>
#include<cstring>
int a[10001],b[10001];
int M,v;
struct tree
{
    int date;
    tree *l,*r;
    tree()
    {
        date=0;
        l=r=NULL;
    }
};
tree* built(int *A,int *B,int n)
{
    if(!n)return NULL;
    tree *now=new tree;
    int i=0;
    for(i=0;i<n;i++)if(A[i]==B[0])break;//找到根在中序遍历中的位置
    if(i>0)now->l=built(A,B+n-i,i);//递归建立左子树
    if(i<n-1)now->r=built(A+i+1,B+1,n-i-1);//递归建立右子树
    now->date=B[0];
    return now;
}
void del(tree *p)
{
    if(!p)return;
    if(p->l)del(p->l);
    if(p->r)del(p->r);
    delete p;
    p=NULL;
}
void dfs(tree *Root,int sum)
{
    if(Root==NULL)return ;
    //printf("%d ",Root->date);
    if(Root->l==NULL&&Root->r==NULL)
    {
        if(sum+Root->date<M)
        {
            M=sum+Root->date;
            v=Root->date;
        }
    }
    dfs(Root->l,sum+Root->date);
    dfs(Root->r,sum+Root->date);
}
int main()
{
    char ch;
    tree *root;
    //freopen("in.txt","r",stdin);
    while(~scanf("%d",&a[0]))
    {
      root=NULL;
      int n=1;
      M=100000001;
      v=0;
      while((ch=getchar())!='\n')
      {
          scanf("%d",a+n);++n;
      }
      for(int i=n-1;i>=0;i--)
      {
           scanf("%d",b+i);
      }
      root=built(a,b,n);
      dfs(root,0);
      printf("%d\n",v);
      del(root);
    }
    return 0;
}

UVA 548(二叉树重建与遍历)

时间: 2024-08-06 07:54:08

UVA 548(二叉树重建与遍历)的相关文章

UVa 548 (二叉树的递归遍历) Tree

题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍历,先父节点  然后左孩子  最后右孩子 中序遍历,先左孩子  然后父节点  最后父节点 后序遍历,先左孩子  然后右孩子  最后父节点 这里有更详细的解释: http://blog.csdn.net/sicofield/article/details/9066987 紫书上面写错了,后序遍历最后一

uva 548(二叉树)

题解:给出了二叉树的中序和后序.重建二叉树.输出路径和最短的叶子的值. 两个模板: 给出前序和中序建树: Node* build (int n, int* preo, int* ino) { Node* node = new Node; int i = 0; if (n <= 0) return NULL; while (ino[i] != preo[0]) i++; node -> val = ino[i]; node -> left = build(i, preo + 1, ino)

UVa 548 Tree(建树,递归遍历)

题意  给你一个树的中序遍历和后序遍历  某个节点的权值为从根节点到该节点所经过节点的和  求权值最小的叶节点的值  如果存在多个  输出值最小的那个 把树建好就好说了  递归递归dfs msun保存最小叶节点权值  ans保存答案 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int maxn = 10005, INF = 0x3f3f3f3f; int i

UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后输出来叶子节点. 一开始写的时候是用gets读入的,报CE, 要用fgets写,关于fgets(),传送门: fgets函数及其用法,C语言fgets函数详解 一开始用bfs过的,后来发现,好多人都是dfs过的,又写了一下dfs... 代码: 1 //二叉树的中序和后序遍历还原树并输出最短路径的叶子

Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path. Input The input

根据二叉树的前序遍历和中序遍历重建二叉树

题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(

重建二叉树与二叉树的层次遍历

数据结构实验之求二叉树后序遍历和层次遍历 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历. 输入 输入数据有多组,第一行是一个整数t (t<1000).代表有t组測试数据.每组包含两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列. 输出 每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列 演示样例输

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

问题描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回. 思路: 在二叉树的前序遍历序列中,第一个数字总是树的根结点的值.但在中序遍历序列中,根结点的值在序列的中间,左子树的结点的值位于根结点的值的左边,而右子树的结点的值位于根结点的值的右边.因此我们需要扫描中序遍历序列,才能找到根结点的值. 如下图所示,

UVA 699(二叉树建树与遍历)

M - The Falling Leaves Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-02-08) Description  The Falling Leaves  Each year, fall in the North Central region is accompanied