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 ...
随机推荐
- Caffe on Mac OS X 10.11
在Mac环境安装Caffe环境(CPU_ONLY) http://blog.csdn.net/xidiancoder/article/details/52081519 有问题 http://blo ...
- SpringBoot之profile详解
SpringBoot中使用配置文件application.properties&application.yml两种方式,在这两种方式下分别对应各自的profile配置方式,同时还存在命令行.虚 ...
- Javascript回调函数中的this指向问题
使用js中的定时器(setInterval,setTimeout),很容易会遇到this指向的问题. 直接上例子: 1 var name = 'my name is window'; 2 var ob ...
- C++ for循环语句
#include "pch.h" #include<iostream> using namespace std; int main() { int i = 1, sum ...
- Centos6与Centos7安装和使用htop
Centos6 安装 rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm r ...
- 169. Majority Element求众数
网址:https://leetcode.com/problems/majority-element/ 参考:https://blog.csdn.net/u014248127/article/detai ...
- C++标准模板库(STL)之Pair
1.Pair的常用用法 pair:两个元素绑在一起作为一个合成元素.可以看成是两个元素的结构体. struct pair { typeName1 first; typeName2 second; }; ...
- 德邦总管 修改oracle数据库用户密码的方法
WIN+R打开运行窗口,输入cmd进入命令行: 输入sqlplus ,输入用户名,输入口令(如果是超级管理员SYS的话需在口令之后加上as sysdba)进入sql命令行: 连接成功后,输入“s ...
- 配置rpm本地源及局域网环境下使用
LInux个人开发过程中可以直接连到公网,所以想要安装各种软件时直接安装即可,但工作环境往往很让人头疼. 如果应用场景是没法链接外网的,公司内部绝大多数情况下是在自己的局域网下玩,这时候想装个软件是相 ...
- lr介绍
---恢复内容开始--- loadrunner是通过agent进程来监控各种协议的客户端和服务端的通信: init和end不能进行迭代,action才能迭代(参数化才有作用) init(比如说有50个 ...