唔,首先这题给出了中序遍历和后序遍历要求我们求出,

一个叶子节点到根的数值总和最小,且这个叶子节点是最小的那个

这题的难点在于如何运用中序遍历和后序遍历还原整棵树,

这里有两个方法:

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的思路的更多相关文章

  1. 【日常学习】【二叉树遍历】Uva548 - Tree题解

    这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子. Uva上不去了,没法測.基本上是依照ruka的代码来的.直接上代码 //Uva548 Tree #inc ...

  2. UVA548——Tree(中后序建树+DFS)

    Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...

  3. [LeetCode] 110. Balanced Binary Tree 解题思路

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  4. UVA548 Tree (二叉树的遍历)

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...

  5. Uva548 Tree

    Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...

  6. 《Note --- Unreal 4 --- behavior tree》

    Web: https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/index.html Test project: D:\En ...

  7. [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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 8.5 GOF设计模式四: 观察者模式Observer

    GOF设计模式四: 观察者模式Observer  现实中遇到的问题  当有许多不同的客户都对同一数据源感兴趣,对相同的数据有不同的处理方式,该如 何解决?5.1 定义: 观察者模式  观察者模式 ...

  2. live 2d js demo

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. python mac 环境配置

    1, Mac安装 HTMLTestRunner,参考:https://blog.csdn.net/walter_chan/article/details/50555123 cd /Library/Py ...

  4. .Net mvc 上传多文件

    .net mvc 上传多文件有很多种方式,我的方法只是其中一种, 仅供参考,我主要是注重参数传递的过程,后面文件保存的地方省略.. 调试环境 vs2017 控制器代码: [HttpPost] publ ...

  5. 笔记《JavaScript 权威指南》(第6版) 系统理论知识概要

    主要内容概要 [JavaScript语言核心]词法结构,类型.值和变量,表达式和运算符,语句,对象,数组,函数,类和模块,正则表达式的模式匹配,JavaScript的子集和扩展,服务器端JavaScr ...

  6. Spring Boot Actuator [监控与管理]

    1. 如何添加 2. actuator 的原生端点(API) 2.1 应用类配置 2.1.1 http://localhost:8080/actuator/conditions 2.1.2 http: ...

  7. EAP-MD5计算方法

    一.说明 领导要求确认设备进行802.1X认证时,是否直接将用户名密码明文传给交换机.配好端口镜像.搭好Radius环境后进行了抓包分析. 二.计算 2.1 802.1X认证过程 完整流程如下: 客户 ...

  8. 彻底搞懂js原型与原型链

    原生的原型链 function fn(){},fn 为 Function的一个实例,原型链为 null -> Object -> Function -> fn: fn.__proto ...

  9. 前端必备之Node+mysql+ejs模版如何写接口

    前端必备之Node+mysql+ejs模版如何写接口 这星期公司要做一个视频的后台管理系统, 让我用Node+mysql+ejs配合写接口, 周末在家研究了一下, 趁还没来具体需求把研究内容在这里分享 ...

  10. CentOS 配置防火墙操作实例

    注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service   iptables status<回车>   停止防火墙: [root@localh ...