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

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

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

这里有两个方法:

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. 阿里云 RDS for MySQL支持什么引擎

    问题:我们的服务器是买的是阿里云,mysql版本5.011 ,本地和服务器配置一样,在本地可以安装discuzX3.4,但是在服务器上却报错了,如下图: 找了半天,才知道阿里云RDS 支持的mysql ...

  2. git reset 和 git revert 使用区别

    git reset 用于回退代码,但是git pull后会和远程分支保持一致,所以无法修改远程代码 git revert可以撤销代码,撤销后直接git push ,可以修改远程分支的代码

  3. vue-quill-editor富文本编辑器,上传图片自定义为借口上传

    vue-quill-editor富文本编辑器,上传图片自定义为借口上传 博客地址:https://blog.csdn.net/lyj2018gyq/article/details/82585194

  4. ie8遇到的那些事

    IE一直是我们津津乐道的浏览器,他的奇葩想必各位在开发之路上都不断的遇到了,其恶心程度就不必说了,我们公司主要是IE的浏览器,这次我就把我遇到的不兼容问题列举下来,欢迎大家补充.此举只发表IE8以上的 ...

  5. 常见Python脚本

    ---恢复内容开始--- 1.请按照这样的日期格式(xxxx-xx-xx)每日生成一个文件,例如今天生成的文件为2019-04-26.log, 并且把磁盘的使用情况写到到这个文件中. import t ...

  6. vmware install win8 and server2012 problem

    Environment: vmware workstation 9 and win7 64bit. Problem: when i want to install server2012r2 and w ...

  7. Elixir东游记/上:intro/1

    1. 为啥前面还在搞haxe,现在又换到elixir了? erlang本来我就在用,用elixir不过是方便顺手给人科普而已. 2. so,接下来你打算用elixir干嘛? 很简单,写一个简单的解释器 ...

  8. Mysql 了解changeBuffer 与 purge 调优

    需要删除.新增记录或更新一个数据页时,如果数据页在内存中就直接更新,而如果这个数据页还没有在内存中的话,在不影响数据一致性的前提下,InooDB 会将这些更新操作缓存在 change buffer中, ...

  9. Ubuntu安装之python开发

    Ubuntu安装之python开发   什么??公司要用Ubuntu(乌班图)?不会用??怎么进行python开发??? 乌班图操作系统下载地址:http://releases.ubuntu.com/ ...

  10. js中关于声明提前的几个误区

    声明提前: 在程序正式执行之前,都会将所有的var声明的变量提前到开始位置,集中创建,而赋值留在原地. 例如这样一段代码 console.log(a) var a = 100; console.log ...