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.

还是二叉树的路径之和,但树的存储方式变了,使用一个三位的数字来存的,百位是该结点的深度,十位是该结点在某一层中的位置,个位是该结点的值。

Python:

class Solution(object):
def pathSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
class Node(object):
def __init__(self, num):
self.level = num/100 - 1
self.i = (num%100)/10 - 1
self.val = num%10
self.leaf = True def isParent(self, other):
return self.level == other.level-1 and \
self.i == other.i/2 if not nums:
return 0
result = 0
q = collections.deque()
dummy = Node(10)
parent = dummy
for num in nums:
child = Node(num)
while not parent.isParent(child):
result += parent.val if parent.leaf else 0
parent = q.popleft()
parent.leaf = False
child.val += parent.val
q.append(child)
while q:
result += q.pop().val
return result

C++:

class Solution {
public:
int pathSum(vector<int>& nums) {
if (nums.empty()) return 0;
int res = 0;
unordered_map<int, int> m;
for (int num : nums) {
m[num / 10] = num % 10;
}
helper(nums[0] / 10, m, 0, res);
return res;
}
void helper(int num, unordered_map<int, int>& m, int cur, int& res) {
int level = num / 10, pos = num % 10;
int left = (level + 1) * 10 + 2 * pos - 1, right = left + 1;
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);
}
};

  

类似题目:

[LeetCode] 112. Path Sum 路径和

[LeetCode] 113. Path Sum II 路径和 II

[LeetCode] 437. Path Sum III 路径和 III

All LeetCode Questions List 题目汇总

  

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

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

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

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

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

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

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

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

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

  7. LeetCode:Minimum Path Sum(网格最大路径和)

    题目链接 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...

  8. 【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 ...

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

随机推荐

  1. 使用scrapy框架爬取全书网书籍信息。

    爬取的内容:书籍名称,作者名称,书籍简介,全书网5041页,写入mysql数据库和.txt文件 1,创建scrapy项目 scrapy startproject numberone 2,创建爬虫主程序 ...

  2. Codeforces I. Inna and Nine(组合)

    题目描述: Inna and Nine time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

    传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...

  4. python 根据字符串语句进行操作再造函数(evec和eval方法)

    例: #coding:utf-8 ''' Created on 2017年9月9日 @author: Bss ''' test_list=['def','a',''] test_list1=['pri ...

  5. 记录一下使用element ui使用级联选择器的坑,级联选择器的默认选中

    Cascader 级联选择器 使用级联选择器我使用的是默认选中值 下面是我的数据格式,只是形式相同,值不同, 后台的数据是这样的不是ID //级联选择器 <el-cascader :props= ...

  6. Dubbo源码分析:ProxyFactory

    roxyFactory将对外开放的服务进行封装.这里使用到代理的方式.ProxyFactory接口有两个不同的实现类:JavassistProxyFactory和JdkProxyFactory.Jdk ...

  7. 铺砖头问题(完美)——爆搜&&插头DP

    题意 给定一个 $n \times m$ 的格子,每个格子被染成了黑色或白色.现在要用 $1 \times 2$ 的砖块覆盖这些格子,要求块与块之间互不重叠,且覆盖了所有白色的格子,但不覆盖任意黑色格 ...

  8. Subarray Sum II

    Description Given an positive integer array A and an interval. Return the number of subarrays whose ...

  9. [Javascript] Sort by multi factors

    For example, we have a 2D arrays; const arys = [ [], [], [] ]; We want to sort by the number first, ...

  10. [ARIA] Add aria-expanded to add semantic value and styling

    In this lesson, we will be going over the attribute aria-expanded. Instead of using a class like .op ...