博客
关于我
【代码超详解】UVA 699 The Falling Leaves 下落的树叶
阅读量:701 次
发布时间:2019-03-21

本文共 1461 字,大约阅读时间需要 4 分钟。

一、题目描述

我们需要处理若干棵二叉树的输入,并通过先序遍历计算每个水平位置落下的叶片数量。每个节点的位置由水平和垂直坐标决定,左子树在水平上减去一,右子树在水平上加一,垂直坐标两边都减去一。同一位置的叶片数量要累加,如果某节点的子树不存在,对应位置值为-1。此次任务需统计每个水平位置的所有节点权值之和,并按顺序输出。

复习先序遍历的定义:

$$PreOrder(N) = Root(N) + PreOrder(NL) + PreOrder(NR)$$

其中,N、NL、NR 分别代表节点、左节点、右节点。运算符+在此代表广义的连接。

二、算法分析说明与代码编写指导

我们采用递归方法构建二叉树,使用一个sum数组记录每一列的叶片总数。初始时,位置从40开始(p=40),注意捕获左右边界L和R。输入结束标记为根节点-1,sum[40]为0时停止。

主程序部分读取输入,调用BuildBinaryTree函数读取每个节点。函数递归处理当前节点,累加到相应位置的sum中,然后处理左右子树。

记录每一层次的水平位置,sum数组大小定为81,覆盖0到80的范围。确保同一位置多次输入则相加,若子树不存在设置值为-1。

三、AC代码

以下代码实现了上述功能:

#include 
#include
#include
using namespace std;#pragma warning(disable:4996)unsigned int v, sum[81], c;unsigned char L, R, p;inline void BuildBinaryTree(const unsigned char &pos) { scanf("%u", &v); if (v == 4294967295) return; sum[pos] += v; if (pos < L) L = pos; if (pos > R) R = pos; BuildBinaryTree(pos - 1); BuildBinaryTree(pos + 1);}int main() { for (;;) { fill(sum, sum + 81, 0); p = 40; L = 40; R = 40; BuildBinaryTree(p); if (sum[40] == 0) break; printf("Case %u:\n%u", ++c, sum[L]); for (unsigned char i = ++L; i <= R; ++i) { printf(" %u", sum[i]); } puts("\n"); } //system("pause"); return 0;}

代码说明:

  • sum数组用于记录各水平位置的叶片总数,从80列(0到80)覆盖所有可能的位置。
  • 递归函数BuildBinaryTree根据输入节点的值,逐个处理当前节点及其左右子树,更新sum数组。
  • 主循环不断读取输入,直到根节点输入为-1时。每次处理后输出当前sum数组范围内的值,确保格式正确。
  • 转载地址:http://tuxez.baihongyu.com/

    你可能感兴趣的文章
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错TypeError: this.getOptions is not a function
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm设置镜像如淘宝:http://npm.taobao.org/
    查看>>
    npm配置安装最新淘宝镜像,旧镜像会errror
    查看>>
    NPM酷库052:sax,按流解析XML
    查看>>
    npm错误 gyp错误 vs版本不对 msvs_version不兼容
    查看>>
    npm错误Error: Cannot find module ‘postcss-loader‘
    查看>>