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:

  1. The hundreds digit represents the depth D of this node, 1 <= D <= 4.
  2. 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.
  3. 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;
}
};

类似题目:

Path Sum III

Binary Tree Maximum Path Sum

Path Sum II

Path Sum

参考资料:

https://discuss.leetcode.com/topic/101111/java-solution-represent-tree-using-hashmap

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Path Sum IV 二叉树的路径和之四的更多相关文章

  1. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  2. [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 ...

  3. [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 ...

  4. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  5. 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 ...

  6. [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. ...

  7. LeetCode Path Sum IV

    原题链接在这里:https://leetcode.com/problems/path-sum-iv/description/ 题目: If the depth of a tree is smaller ...

  8. LeetCode Path Sum 判断树的路径之和

    /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; ...

  9. [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. ...

随机推荐

  1. 实现Windows程序的数据绑定

    1.创建DataSet对象 语法: DataSet  数据集对象  =new  DataSet("数据集的名称字符串"); 语法中的参数是数据集的名称字符串,可以有,也可以没有.如 ...

  2. QuietHit小Game

    根据项目的要求分别建出几个类 有游戏类 玩家类 测试类 等级类 等级时间类 一以下类图: 游戏类: public class Game { private Player player; public ...

  3. LeetCode-391. 完美矩形(使用C语言编译,详解)

    链接:https://leetcode-cn.com/problems/perfect-rectangle/description/ 题目 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, ...

  4. 记录某公司(简称SMKJ) 的一次面试

    昨天去了一家公司面试 Java 开发岗位,这篇文章主要是做一个面试的记录以及总结. 这家公司的规模大概100-200人,环境还可以,在一栋大厦租了两层办公室(31层和32层).一同搭电梯上去的还有一位 ...

  5. 海外仓系统 COD货到付款到付功能

    全球还有很多国家买家网购选择货到付款方式,例如东南亚的越南.泰国.印度尼西亚,中东的阿联酋.沙特等国家.在这些国家建立海外仓需要需要具备COD货到付款功能,麦哲伦海外仓系统已经支持COD货到到付结算相 ...

  6. 每日冲刺报告——Day2(Java-Team)

    第二天报告(11.3  周五) 团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://git ...

  7. 下载文件downloadFile

    public static void downLoadFile(InputStream inStream, String fileName) { if (StringUtils.isBlank(fil ...

  8. 【审核】检查iOS项目中是否使用了IDFA

    (1)什么是IDFA 关于IDFA,在提交应用到App Store时,iTunes Connect有如下说明: 这里说到检查项目中是否包含IDFA,那如何来对iOS项目(包括第三方SDK)检查是否包含 ...

  9. bzoj千题计划165:bzoj5127: 数据校验

    http://www.lydsy.com/JudgeOnline/upload/201712/prob12.pdf 区间的任意一个子区间都满足值域连续 等价于 区间任意一个长为2的子区间都满足值域连续 ...

  10. ubuntu启动报/root/.profile mesg:ttyname failed错误的解决办法

    修改/root/.profile文件,如下命令 sudo gedit /root/profile 将文中的最后一行mesg n修改成tty -s && mesg n