[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-digits integers.
For each integer in this list:
- The hundreds digit represents the depth
D
of this node,1 <= D <= 4.
- The tens digit represents the position
P
of 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
V
of this node,0 <= V <= 9.
Given a list of ascending
three-digits integers representing a binary 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.
这道题还是让我们求二叉树的路径之和,但是跟之前不同的是,树的存储方式比较特别,并没有专门的数结点,而是使用一个三位数字来存的,百位数是该结点的深度,十位上是该结点在某一层中的位置,个位数是该结点的结点值。为了求路径之和,我们肯定还是需要遍历树,但是由于没有树结点,所以我们可以用其他的数据结构代替。比如我们可以将每个结点的位置信息和结点值分离开,然后建立两者之间的映射。比如我们可以将百位数和十位数当作key,将个位数当作value,建立映射。由于题目中说了数组是有序的,所以首元素就是根结点,然后我们进行先序遍历即可。在递归函数中,我们先将深度和位置拆分出来,然后算出左右子结点的深度和位置的两位数,我们还要维护一个变量cur,用来保存当前路径之和。如果当前结点的左右子结点不存在,说明此时cur已经是一条完整的路径之和了,加到结果res中,直接返回。否则就是对存在的左右子结点调用递归函数即可,参见代码如下:
解法一:
class Solution {
public:
int pathSum(vector<int>& nums) {
if (nums.empty()) return ;
int res = ;
unordered_map<int, int> m;
for (int num : nums) {
m[num / ] = num % ;
}
helper(nums[] / , m, , res);
return res;
}
void helper(int num, unordered_map<int, int>& m, int cur, int& res) {
int level = num / , pos = num % ;
int left = (level + ) * + * pos - , right = left + ;
cur += m[num];
if (!m.count(left) && !m.count(right)) {
res += cur;
return;
}
if (m.count(left)) helper(left, m, cur, res);
if (m.count(right)) helper(right, m, cur, res);
}
};
下面这种方法是迭代的形式,我们使用的层序遍历,与先序遍历不同的是,我们不能维护一个当前路径之和的变量,这样会重复计算结点值,而是在遍历每一层的结点时,加上其父结点的值,如果某一个结点没有子结点了,才将累加起来的结点值加到结果res中,参见代码如下:
解法二:
class Solution {
public:
int pathSum(vector<int>& nums) {
if (nums.empty()) return ;
int res = , cur = ;
unordered_map<int, int> m;
queue<int> q{{nums[] / }};
for (int num : nums) {
m[num / ] = num % ;
}
while (!q.empty()) {
int t = q.front(); q.pop();
int level = t / , pos = t % ;
int left = (level + ) * + * pos - , right = left + ;
if (!m.count(left) && !m.count(right)) {
res += m[t];
}
if (m.count(left)) {
m[left] += m[t];
q.push(left);
}
if (m.count(right)) {
m[right] += m[t];
q.push(right);
}
}
return res;
}
};
类似题目:
参考资料:
https://discuss.leetcode.com/topic/101111/java-solution-represent-tree-using-hashmap
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Path Sum IV 二叉树的路径和之四的更多相关文章
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [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] 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] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- [Leetcode] Binary tree maximum path sum求二叉树最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- LeetCode Path Sum IV
原题链接在这里:https://leetcode.com/problems/path-sum-iv/description/ 题目: If the depth of a tree is smaller ...
- LeetCode Path Sum 判断树的路径之和
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
随机推荐
- 介绍C语言指针
最近心态不太好,但是还是控制自己刷一下算法题,但是看着多次出现的 “Segmentation fault”,心态又爆炸啦.我只想说:“我也早觉得有写一点东西的必要了.离三月十八日也已有两星期,忘却的救 ...
- [css 实践篇] 解决悬浮的<header> <footer>遮挡内容的处理技巧
我写的实践篇 都是自己在实践项目所遇到的 "拦路虎" 还是很有借鉴的意义的.(实践才是检验真理的唯一标准呀),废话不多说,进去正题 position: fixed 绝对固定底部后会 ...
- 关于VR开发中的穿墙问题随想
在VR开发中,用户将以第一人称的视角进入虚拟世界,即用户同时身处两个坐标系:1. 现实世界坐标系(如房间的坐标系),用户的身体处于这个坐标系 2. VR世界坐标系,用户的感官处于这个坐标系,即用户觉得 ...
- 查看端口使用情况(lsof,netstat, kill)
在Mac上查看端口使用情况只能使用lsof(list open file),无法使用 netstat. 查看某个端口是否正在被占用: lsof -i:portno 另外,可以通过: lsof 指令来查 ...
- Java基础学习笔记二十一 多线程
多线程介绍 学习多线程之前,我们先要了解几个关于多线程有关的概念.进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. 线 ...
- 通过cmd窗口导入导出mysql数据库
1.导入数据库 使用source命令 首先要在cmd窗口中连接数据库,然后再用source命令进行导入操作 mysql>use 数据库名 mysql>source d:/dbname.sq ...
- 第五次作业-需求&原型改进
需求&原型改进 0. 团队介绍 团队名称:121ComeOn 项目名称:个人博客项目 团队组成: PM:黄金筱(107) 成员:王枫(031),刘烨(255),周明浩(277) github地 ...
- 20162311张之睿 Linux基础与Java开发环境实验报告
实验一 Java开发环境的熟悉 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没有Linux基础的同学建议先学习& ...
- 算法第四版学习笔记之优先队列--Priority Queues
软件:DrJava 参考书:算法(第四版) 章节:2.4优先队列(以下截图是算法配套视频所讲内容截图) 1:API 与初级实现 2:堆得定义 3:堆排序 4:事件驱动的仿真 优先队列最重要的操作就是删 ...
- Scala 操作符与提取器
实际上Scala没有操作符, 只是以操作符的格式使用方法. 操作符的优先级取决于第一个字符(除了赋值操作符), 而结合性取决于最后一个字符 Scala的操作符命名更加灵活:) 操作符 中置操作符(In ...