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

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

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

这里有两个方法:

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. 什么是卷积convolution

    定义 卷积是两个变量在某范围内相乘后求和的结果.如果卷积的变量是序列x(n)和h(n),则卷积的结果 , 其中星号*表示卷积. 当时序n=0时,序列h(-i)是h(i)的时序i取反的结果:时序取反使得 ...

  2. js统一设置富文本中的图片宽度

    var txt = layedit.getContent(ieditor);//获取编辑器内的文本var regex = new RegExp('<img', 'gi');txt = txt.r ...

  3. oralce 常用sql

    查看表空间及其数据文件 SELECT * FROM dba_data_files; 查看用户下的表SELECT * FROM user_tables; 查看用户表的注释SELECT * FROM us ...

  4. vue.js 自带阻止默认事件 阻止冒泡

    <!-- 阻止单击事件冒泡 --> <a v-on:click.stop="doThis"></a>   <!-- 提交事件不再重载页面  ...

  5. mybatis源码数据库链接配置

    <?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE configuration        ...

  6. idea新建maven工程没有artifacts

    原理,是因为没把新创建好的maven项目给设置成一个可被tomcat部署的web项目 选择个项目路径 配置artifacts,引入到tomcat就可以

  7. python中os模块在windows下的使用

    今天学习了一下Python的os模块,主要是针对文件夹和文件路径的一系列操作. 与Python内置函数相比这里这里的函数功能更多样化,功能也更强大.但是学习过程中我发现很多函数都是只适用于unix系统 ...

  8. unity5.6 导出gradle工程,Android Studio 导入问题以及解决

    导入后gradle building 一直到跑,卡住了,一般是gradle没有下载,又下不下来的原因. 去  http://services.gradle.org/distributions/  下载 ...

  9. HTML DOM 的nodeType属性

    在HTML DOM中每一部分都是节点: HTML元素是元素节点 HTML中属性是属性节点 文本是文本节点 注释是注释节点 这时我们要给它区分开我们就可以使用HTML DOM的nodeType属性 no ...

  10. 一个简单的Quartz定时任务

    package com.shuadan.quartz; import org.springframework.scheduling.annotation.Scheduled; import org.s ...