当前位置: 首页 > news >正文

UVa 548 Tree(建树+DFS)

题目链接

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.

Input
The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.

Output
For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.

1.这种多文件的多行输入真的容易把人搞懵,书上的做法挺值得学习的。stringstream建议去自己学一下

2.另外就是如何去建树,书上写的是两个节点数组保存着左右孩子,传入根节点后DFS搜索。这样做的原理是本题中声明了每个节点的权值都不相同。笔者感觉还是按链表来写吧,这样更易懂更具广泛性,下文代码就是按链表来写的

3.DFS这个函数一定要搞懂,不能只知道原理却不知道如何去写函数。刚开始看不懂,想了一大会,sum是一个局部变量,在每次深搜到叶子时,从上一个非叶子节点的sum并没有变化,当返回搜右孩子时,又是一个新的权值和。这就是递归函数的奥妙所在。多思考多理解,以后DFS和BFS会很常用的,建议熟练掌握。

代码:

#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;
#define INF 0x7fffffff	//刚开始记错了INF的值写成了0xffffffff-1,但这是-1,导致调试了好久
typedef struct BinaryTree{
    int data;
    struct BinaryTree *lchild,*rchild;
    BinaryTree():data(0),lchild(nullptr),rchild(nullptr){}
}*BiNode;
BiNode newNode(){ return new BinaryTree(); } //不写该函数也可
const int maxn=1e4+10;
int in_order[maxn],post_order[maxn];
int num,cnt,res;
BiNode root;
bool read_line(int *a){
    string s;
    if(!getline(cin,s)) return false;
    stringstream ss(s);
    int x;
    num=0;
    while(ss>>x) a[++num]=x;
    return num>0;
}
BiNode buildTree(int b1,int e1,int b2,int e2){
    if(b1>e1) return nullptr;
    BiNode r=newNode();
    r->data=post_order[e2];
    int k=b1;
    while(in_order[k]!=r->data) k++;
    int res=k-b1;
    r->lchild=buildTree(b1,k-1,b2,b2+res-1);
    r->rchild=buildTree(k+1,e1,b2+res,e2-1);
    return r;
}
void DFS(BiNode T,int sum){
    sum+=T->data;
    if(!(T->lchild) && !(T->rchild)){
        if(sum<res||(sum==res&&T->data<cnt)){
            cnt=T->data;
            res=sum;
        }
    }
    if(T->lchild) DFS(T->lchild,sum);
    if(T->rchild) DFS(T->rchild,sum);
}
/*void PreOrderTraverse(BiNode T) //先序遍历一下树
{
    if(T==nullptr)
        return;
    printf("%d ",T->data);
    PreOrderTraverse(T->lchild);
    PreOrderTraverse(T->rchild);
}*/
void freeMemory(BiNode T){
    if(T==nullptr) return;
    freeMemory(T->lchild);
    freeMemory(T->rchild);
    delete T;
}
int main()
{
    while(read_line(in_order)){
        read_line(post_order);
        //每次循环后一定别忘了对全局变量的处理,一些赋值或者清0都得再来
        res=INF;
        root=buildTree(1,num,1,num);
        //PreOrderTraverse(root);
        DFS(root,0);
        printf("%d\n",cnt);
        freeMemory(root);
    }
    return 0;
}

相关文章:

  • Android开发指南-框架主题-安全和许可
  • UVa 699 The Falling Leaves(建树+求竖直权值和)
  • Widget带来了真正的移动互联网
  • 2019 ICPC 徐州区域赛 - C <3 numbers(素数密度)
  • 2019 ICPC 徐州区域赛 - A Cat(异或性质)
  • 2019 ICPC 南昌区域赛 - C And and Pair(思维+组合数学)
  • Android开发指南-框架主题-清单文件
  • 2019 ICPC 南昌区域赛 - G Eating Plan(技巧+暴力)
  • 离职的日子
  • 2019 ICPC 南京区域赛 - A A Hard Problem(找规律)
  • 走向架构师之路---开博寄语
  • JavaWeb拦截器拦截了静态资源的解决办法
  • 职业方向的选择
  • 2019 ICPC 南京区域赛 - H Prince and Princess(博弈+思维)
  • M2M----物联网的未来值得期待
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • Angular 响应式表单之下拉框
  • Django 博客开发教程 16 - 统计文章阅读量
  • IIS 10 PHP CGI 设置 PHP_INI_SCAN_DIR
  • IP路由与转发
  • nginx 配置多 域名 + 多 https
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • Python中eval与exec的使用及区别
  • 漂亮刷新控件-iOS
  • 如何优雅地使用 Sublime Text
  • 算法-插入排序
  • 听说你叫Java(二)–Servlet请求
  • 小李飞刀:SQL题目刷起来!
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • Hibernate主键生成策略及选择
  • ionic入门之数据绑定显示-1
  • k8s使用glusterfs实现动态持久化存储
  • 带你开发类似Pokemon Go的AR游戏
  • #laravel 通过手动安装依赖PHPExcel#
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • (1)bark-ml
  • (八)c52学习之旅-中断实验
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (南京观海微电子)——I3C协议介绍
  • (算法)N皇后问题
  • (转)socket Aio demo
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .gitattributes 文件
  • .NET成年了,然后呢?
  • .Net的C#语言取月份数值对应的MonthName值
  • .net实现头像缩放截取功能 -----转载自accp教程网
  • .NET实现之(自动更新)
  • .NET项目中存在多个web.config文件时的加载顺序
  • .NET中的十进制浮点类型,徐汇区网站设计
  • .vimrc php,修改home目录下的.vimrc文件,vim配置php高亮显示
  • @ 代码随想录算法训练营第8周(C语言)|Day53(动态规划)
  • @Builder用法