UVA548 tree的思路
唔,首先这题给出了中序遍历和后序遍历要求我们求出,
一个叶子节点到根的数值总和最小,且这个叶子节点是最小的那个
这题的难点在于如何运用中序遍历和后序遍历还原整棵树,
这里有两个方法:
1. 递归构造原树。
1. 运用链表构造一棵树。
我将要运用的是链表构造树。
#include<bits/stdc++.h>
using namespace std;
struct Node{
int v;
Node *left,*right;
};//首先利用结构体构造树的节点
Node *root;//申请根节点
Node *newnode() {return new Node();}//添加新的节点
const int inf=0x7fffffff;//设定值最大
const int maxn=+;
int p[maxn],i[maxn],n,ok,b,best;
bool read(int *a)//读入数据,进行存储
{
string s;
if(!getline(cin,s)) return false;//输入进树的节点数
stringstream tf(s);
int x;n=;
while(tf>>x) a[n++]=x;
return n>;
}
Node* bulid(int l1,int r1,int l2,int r2)//构造树的节点
{
if(l1>r1) return NULL;//null==空,所以空数返回空
Node *t=newnode();//申请节点
t->v=p[r2];//运用链表
int p=l1;
while(i[p]!=t->v) p++;//添加子树
int cnt=p-l1;
t->left=bulid(l1,p-,l2,l2+cnt-);
t->right=bulid(p+,r1,l2+cnt,r2-);//构造左右子树
return t;
}
void dfs(Node *t,int sum)//用深搜寻找最小值的终端叶子
{
sum+=t->v;
if(!t->left&&!t->right){//检寻左右子树
if(sum<b||(sum==b&&t->v<best)){
b=sum;best=t->v;
}
}
if(t->left) dfs(t->left,sum);
if(t->right) dfs(t->right,sum);//搜索回溯
}
int main()
{
while(read(i)){//输入
read(p);
b=inf;best=inf;
root=bulid(,n-,,n-);
dfs(root,);//从根节点开始搜
printf("%d\n",best);
}
}
就是这个样子啦!!!
UVA548 tree的思路的更多相关文章
- 【日常学习】【二叉树遍历】Uva548 - Tree题解
这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子. Uva上不去了,没法測.基本上是依照ruka的代码来的.直接上代码 //Uva548 Tree #inc ...
- UVA548——Tree(中后序建树+DFS)
Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...
- [LeetCode] 110. Balanced Binary Tree 解题思路
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- UVA548 Tree (二叉树的遍历)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- Uva548 Tree
Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...
- 《Note --- Unreal 4 --- behavior tree》
Web: https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/index.html Test project: D:\En ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- leetcode105:Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- Java for LeetCode 173 Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- windows下载mysql5.7压缩包配置安装mysql
windows安装mysql5.7有两种方式 1.下载.msi安装文件直接根据界面提示进行安装 2.下载.biz或.zip压缩包 注:本次在win10系统下使用压缩包进行安装mysql5.7 mysq ...
- nameode启动过程
namenode在内存和磁盘中都保存了fsimage和edits文件 内存中保证hdfs文件系统的访问效率,磁盘中保证hdfs文件系统的安全性 namenode的文件组成: fsimage文件:保存文 ...
- 【lintcode】 二分法总结 I
二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...
- 构建web应用之——文件上传
我们通过使用multipart请求数据接收和处理二进制信息(如文件).DispatcherServlet并没有实现任何解析multipart请求数据的功能,它将该任务委托给了Spring中的multi ...
- 在本地运行正常的静态网页放到tomcat中却显示异常的原因
在本地写好了一个个人网站,本地直接用浏览器运行,很顺利,然而把网站放到Tomcat里面,却发现图片显示不出来,这就奇怪了. 后来发现,我的网站的Image文件夹用了大写“I”,而网页里面的路径用了小写 ...
- java使用格式String型转成Date型
public class TimeTwo { public static void main(String[] args) throws ParseException{ String s = &quo ...
- linux 线程详解
线程 是计算机中独立运行的最小单位,运行时占用很少的系统资源.可以把线程看成是操作系统分配CPU时间的基本单元.一个进程可以拥有一个至多个线程.它线程在进程内部共享地址空间.打开的文件描述符等资源.同 ...
- xpath 在firefox,chrome中正常,在requests中不正常的解决。
经多次测试发现: lxml中的etree格式化以后,直接使用firefox或chrome提取的xpath检索不到内容. 主要是因为tbody # 车种xpathczxx = '/html/body/t ...
- linux 常见报错
yum install 包名 出现安装包重复,同一个安装包出现多版本 使用 rpm -qa |grep 包名 如果出现包名,则说明已存在该包(已被安装),要安装新版本的,可以卸载已装的y ...
- emacs技巧
Table of Contents "ctrl space"设置mark 同时在选中的各行行首添加相同内容 寄存器 跳转到某行 删除光标所在的空格和TAB或空白行 把TAB全部转换 ...