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

题目大意:

    输入一个二叉树的中序和后序,输出一个叶子节点,该叶子节点到根的数值总和最小。

解题思路:

    先通过后序和中序建立二叉树,在通过DFS进行搜索,找到符合题目要求的叶子。(需要使用全局变量来记录DFS过程中的最小和叶子)

    中序和后序建立二叉树:

      使用递归来逐步建立,由后序连确定当前递归中的分支的根节点,再在中序中找到根的位置,则中序中根左的为左子树的中序排列,根右的为右子树的中序。设此时左子树的长度为len,则当前的后序的前len个数据是左子树的后序排列。同理进行递归即可。右子树同理。

    DFS:

      设一个变量m,每次递归时作为实参进入调用,并执行m+=tree.data,则能保证递归到叶子节点时,m保存的是当前叶子到根节点的和,根据m的大小,即可选出符合题意的叶子节点。

Code:

 #include<malloc.h>
#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
using namespace std;
struct tree
{
int data;
int left;
int right;
} T[]; //数组模拟的二叉树
int m_sum=,pos; //用于DFS时保存结果
int mid[],last[];
int create_tree(int m1,int m2,int l1,int l2)
{
if (m1==m2)
{
T[m1].left=T[m1].right=-;
T[m1].data=mid[m1];
return m1;
}
if (m1>m2)
return -;
int i;
for (i=; i<=m2; i++)
if (mid[i]==last[l2]) break;
T[i].left=create_tree(m1,i-,l1,l1+i-m1-);//递归建树!!注意四个参数,runtime好多遍
T[i].right=create_tree(i+,m2,l1+i-m1,l2-);
T[i].data=mid[i];
return i;
}
int min(int a,int b)
{
return a>b?b:a;
}
int dfs(int head,int m)
{
m+=T[head].data;
if (T[head].left==-&&T[head].right==-)
{
if (m<m_sum) //打擂台,选出最小的结果,并将叶子节点的数值存入pos中
{
m_sum=m;
pos=T[head].data;
}
return T[head].data;
}
int sum=T[head].data;
if (T[head].left!=-&&T[head].right==-) sum+=dfs(T[head].left,m);
else if (T[head].right!=-&&T[head].left==-) sum+=dfs(T[head].right,m);
else sum+=min(dfs(T[head].left,m),dfs(T[head].right,m));
return sum;
}
int main()
{
int k1=,k2=;
while (scanf("%d",&mid[])!=EOF)
{
pos=,m_sum=;
k1=;
for (int i=; i<=; i++)
T[i].left=T[i].right=-,T[i].data=;
while ()
{
char ch=getchar();
if (ch=='\n') break;
scanf("%d",&mid[k1++]);
}
k1--,k2=;
while ()
{
scanf("%d",&last[k2++]);
char ch=getchar();
if (ch=='\n') break;
}
k2--;
int T_head=create_tree(,k1,,k2);
dfs(T_head,);
printf("%d\n",pos);
}
return ;
}

UVA548——Tree(中后序建树+DFS)的更多相关文章

  1. [C++] 非递归实现前中后序遍历二叉树

    目录 前置技能 需求描述 binarytree.h 具体实现 binarytree.cpp main.cpp 网上代码一搜一大片,大同小异咯. 书上的函数实现代码甚至更胜一筹,而且抄一遍就能用,唯一问 ...

  2. 前中后序递归遍历树的体会 with Python

    前序:跟->左->右 中序:左->根->右 后序:左>右->根 采用递归遍历时,编译器/解释器负责将递归函数调用过程压入栈并保护现场,在不同位置处理根节点即可实现不 ...

  3. PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...

  4. PAT甲题题解-1127. ZigZagging on a Tree (30)-中序、后序建树

    根据中序遍历和前序遍历确定一棵二叉树,然后按“层次遍历”序列输出.输出规则:除根节点外,接下来每层的节点输出顺序是:先从左到右,再从右到左,交替输出 #include <iostream> ...

  5. Binary Tree Traversal 二叉树的前中后序遍历

    [抄题]:二叉树前序遍历 [思维问题]: 不会递归.三要素:下定义.拆分问题(eg root-root.left).终止条件 [一句话思路]: 节点非空时往左移,否则新取一个点 再往右移. [输入量] ...

  6. POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)

    链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...

  7. 【紫书】Tree UVA - 548 静态建树dfs

    题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...

  8. 数据结构-C语言递归实现树的前中后序遍历

    #include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...

  9. 飘逸的python - 极简的二叉树前中后序通杀函数

    对于任一结点.能够按某种次序运行三个操作: 訪问结点本身(N) 遍历该结点的左子树(L) 遍历该结点的右子树(R) 用来表示顺序,即,前序NLR/中序LNR/后序LRN. 以下我们用namedtupl ...

随机推荐

  1. poj 2431 Expedition

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12980   Accepted: 3705 Descr ...

  2. Poj 1328 / OpenJudge 1328 Radar Installation

    1.Link: http://poj.org/problem?id=1328 http://bailian.openjudge.cn/practice/1328/ 2.Content: Radar I ...

  3. 用一天的时间学习Java EE中的SSH框架

    首先说明一下,本人目前主要从事.NET领域的工作,但对于C++.Java.OC等语言也略知一二,周末闲来无事,特花费一天的时间学习了一下Java中的SSH框架,希望把学习过程中的心得体会与园友们进行分 ...

  4. centos7搭建NIS与NFS综合应用

    实验环境: centos7(服务端)        redhat enterprise linux 7.2(客户端) 实验目的:用centos7的账号,能在redhat enterprise linu ...

  5. 【转】winform与web 按钮button去掉边框

    ref:http://blog.csdn.net/wangzh300/article/details/5264316 WinForm的话 设置Button属性的FlatStyle为Flat,并且设置F ...

  6. 解决BLOB/TEXT column can't have a default value query问题

    Create table的时候,报错BLOB/TEXT column 'xxxxxx( 表名称)' can't have a default value query ,意思是TEXT类型的表字段不能够 ...

  7. Jqplot在joomla组件中的应用

    (1)在com_collect组件中采用的是ajax获取json类型的值.[http://www.jqplot.com/tests/data-renderers.php]这上边有实例. (2)在jqp ...

  8. [Python 标准库]第一章 文本

    Chapter01 文本 1.1 string - 文本常量和模板 作用:包含处理文本的常量和类. 1.1.1 函数 capwords(s):字符串中所有单词首字母大写 maketrans():创建转 ...

  9. Python数据结构——二叉树的实现

    1. 二叉树 二叉树(binary tree)中的每个节点都不能有多于两个的儿子. 1.1 二叉树列表实现 如上图的二叉树可用列表表示: tree=['A', #root ['B', #左子树 ['D ...

  10. Oracle分析函数 — rank, dense_rank, row_number用法

    本文通过例子演示了Oracle分析函数 —— rank, dense_rank, row_number的用法. //首先建score表 create table score( course   nva ...