【LeetCode】666. Path Sum IV 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/path-sum-iv/
题目描述
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digits integers.
For each integer in this list:
- The hundreds digit represents the depth
Dof this node,1 <= D <= 4. - The tens digit represents the position
Pof this node in the level it belongs to,1 <= P <= 8. The position is the same as that in a full binary tree. - The units digit represents the value
Vof this node,0 <= V <= 9.
Given a list of ascending three-digits integers representing a binary tree with the depth smaller than 5, you need to return the sum of all paths from the root towards the leaves.
Example 1:
Input: [113, 215, 221]
Output: 12
Explanation:
The tree that the list represents is:
3
/ \
5 1
The path sum is (3 + 5) + (3 + 1) = 12.
Example 2:
Input: [113, 221]
Output: 4
Explanation:
The tree that the list represents is:
3
\
1
The path sum is (3 + 1) = 4.
题目大意
给定一个包含三位整数的升序数组,表示一棵深度小于 5 的二叉树,请你返回从根到所有叶子结点的路径之和。
解题方法
DFS
这个题很新颖,很少见。我们知道树的表示方法有两种,一种是链表方式,一种是数组方式。这个题考的就是我们的数组方式。数组方式也是很有用的,比如在堆排序中,就很实用。

在一个数组表示的树中,如果一个节点的索引是index,那么其左孩子是index * 2,右孩子是index * 2 + 1。
我们先把给出的nums数组进行拆解,把每个数放入数组中对应的节点中。数组的默认数字是-1,也就是说-1表示空节点。
再计算带权路径和的时候,需要找到每个叶子节点的路径,判断是否是叶子节点的方法是看其两个孩子的值是不是都是-1。题目给定的数组的深度是5,那么最多有32个叶子节点,为了方便叶子节点的判断,选择了大小为64的数组。
如果一个节点是叶子节点,那么累加路径长度到最后的结果中就行了。
C++代码如下:
class Solution {
public:
int pathSum(vector<int>& nums) {
vector<int> tree(64, -1);
for (int n : nums) {
int v = n % 10; n /= 10;
int p = n % 10; n /= 10;
int d = n % 10;
tree[(int)pow(2, d - 1) + p - 1] = v;
}
dfs(tree, 1, 0);
return sum;
}
void dfs(vector<int>& tree, int index, int parent) {
if (tree[index] == -1) return;
if (tree[index * 2] == -1 && tree[index * 2 + 1] == -1) {
sum += parent + tree[index];
return;
}
dfs(tree, index * 2, parent + tree[index]);
dfs(tree, index * 2 + 1, parent + tree[index]);
}
private:
int sum = 0;
};
日期
2019 年 9 月 24 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪
【LeetCode】666. Path Sum IV 解题报告 (C++)的更多相关文章
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【leetcode】Path Sum IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- 【LeetCode】437. Path Sum III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...
- 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- [LeetCode] 113. Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 112. Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- Git 使用,本地项目上传到GitHub远程库
Git 使用,本地项目上传到GitHub远程库 环境 GitHub账号 点此进入github官网 git客户端工具 点此进入git下载页 本地项目上传到 GitHub 在GitHub中创建一个仓库(远 ...
- 【基因组组装】HiC挂载Juicebox纠错补充
目录 1. 主要纠错类型 misjoins translocations inversions chromosome boundaries 2. 其他有用操作 撤销与反撤销 移到边角料 1. 主要纠错 ...
- R 语言实战-Part 5-1笔记
R 语言实战(第二版) part 5-1 技能拓展 ----------第19章 使用ggplot2进行高级绘图------------------------- #R的四种图形系统: #①base: ...
- Docker将容器制作成镜像并提交到远程仓库
Docker将容器制作成镜像并提交到远程仓库 步骤如下 先在dockerhub上创建一个自己的用户https://hub.docker.com/.或者在阿里云也可以. 2. 然后先创建一个空的镜像名. ...
- /etc/sudoers 文件
sudo的权限控制可以在/etc/sudoers文件中查看到 如果想要控制某个用户(或某个组用户)只能执行root权限中的一部分命令, 或者允许某些用户使用sudo时不需要输入密码 格式一般都是 ro ...
- Linux—yum的python版本错误——初级解决方案
为了安装rrdtool,发现不是少这个就是少那个,最后发现yum也不能用. 从网上找的解决yum问题. 转自:http://doarthon.blog.51cto.com/3175384/728809 ...
- Apache RocketMQ分布式消息传递和流数据平台及大厂面试宝典v4.9.2
概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Apache RocketMQ官网地址 https://rocketmq.apache.org/ Latest rel ...
- Tomcat类加载机制和JAVA类加载机制的比较
图解Tomcat类加载机制 说到本篇的tomcat类加载机制,不得不说翻译学习tomcat的初衷. 之前实习的时候学习javaMelody的源码,但是它是一个Maven的项目,与我们自己的 ...
- 学习java的第十三天
一.今日收获(前两天家里有事,博客都忘了发了,唉) 1.通过看哔哩哔哩看黑马程序员的教学视频,学习了java中的数据类型自动转换.强制转换及注意事项三节 2.简单看了看完全学习手册 二.今日问题 1. ...
- 大数据学习----day27----hive02------1. 分桶表以及分桶抽样查询 2. 导出数据 3.Hive数据类型 4 逐行运算查询基本语法(group by用法,原理补充) 5.case when(练习题,多表关联)6 排序
1. 分桶表以及分桶抽样查询 1.1 分桶表 对Hive(Inceptor)表分桶可以将表中记录按分桶键(某个字段对应的的值)的哈希值分散进多个文件中,这些小文件称为桶. 如要按照name属性分为3个 ...