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 ...
随机推荐
- Mac OS X 操作系统下JDK安装与环境变量配置
1. 下载JDK. 去oracle官网的Java SE Downloads页面(如图 1),下载Mac os版本JDK(如图 2): 图 1 图 2 2. 安装JDK. 下载完成后,双击.dmg文 ...
- Linux 下各文件夹的含义
/bin 该目录中存放Linux的常用命令./boot 该目录默认下存放的是Linux的启动文件和内核./cdrom 该目录在刚安装系统时是空的,你可以将光驱文件系统挂在这个目录下./dev 该目录包 ...
- 网络爬虫&起点中文网完本榜500部小说
# 网络爬虫爬取起点中文网完本榜小说500部# 四步,分步操作,不易出错# 所需要获取的数据:书名 .作者.网址.类型.主要介绍.作品信息 from urllib.request import * # ...
- 生成SQL Server数据字典
1.表信息 Select * FROM INFORMATION_SCHEMA.COLUMNS order by Table_name; select * from INFORMATION_SCHEMA ...
- Spring教程笔记(3)
getBean() ApplicationContext接口获取Bean方法简介: • Object getBean(String name) 根据名称返回一个Bean,客户端需要自己进行类型转换: ...
- C++标准模板库(STL)之String
1.String的常用用法 在C语言中,使用字符数组char str[]来存字符串,字符数组操作比较麻烦,而且容易有'\0'的问题,C++在STL中加入string类型,对字符串常用的需求功能进行封装 ...
- Gson反序列化Map,整型自动转换为浮点型
一 坑 场景:将Map<String,Object>结果序列化后放入redis缓存,发现反序列化后Integer类型自动转换成了Double类型 二 测试重现 @Test public v ...
- uva 10288 gailv
Problem F Coupons Input: standard input Output: standard output Time Limit: seconds Memory Limit: MB ...
- 像调试java一样来调试Redis lua
高并发的系统中,redis的使用是非常频繁的,而lua脚本则更是锦上添花.因为lua脚本本身执行的时候是一个事务性的操作,不会掺杂其他外部的命令,所以很多关键的系统节点都会用redis+lua来实现一 ...
- 【调试基础】Part 5 PE格式
PE概念.区块分类