原题链接在这里:https://leetcode.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:

  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.

题解:

每个数字的前两位就决定了node所在的level 和 position.

那么这个node的left child 所在位置是 level + 1, 2*position-1. right child 所在位置是 level + 1, 2*position.

把所有node的位置都存起来, 当走到一个node, 它的left 和 right child 都没有记录的时候就说明走到了叶子节点, 此时记录的path sum可以累积到结果中.

Time Complexity: O(n). n是tree 的node数目.

Space: O(n).

AC Java:

 class Solution {
int sum = 0; public int pathSum(int[] nums) {
if(nums == null || nums.length == 0){
return sum;
} HashMap<Integer, Integer> hm = new HashMap<>();
for(int num : nums){
hm.put(num / 10, num % 10);
} dfs(nums[0] / 10, hm, 0);
return sum;
} private void dfs(int rootKey, Map<Integer, Integer> hm, int cur){
if(!hm.containsKey(rootKey)){
return;
} cur += hm.get(rootKey); int level = rootKey / 10;
int pos = rootKey % 10;
int leftKey = (level + 1) * 10 + 2 * pos - 1;
int rightKey = leftKey + 1; if(!hm.containsKey(leftKey) && !hm.containsKey(rightKey)){
sum += cur;
return;
} dfs(leftKey, hm, cur);
dfs(rightKey, hm, cur);
}
}

类似Path Sum III.

LeetCode Path Sum IV的更多相关文章

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

  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] 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] Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  5. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

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

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

  8. 【LeetCode】666. Path Sum IV 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

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

随机推荐

  1. 如何选择单片机和Android-LInux-ARM开发板?

    源: 如何选择单片机和Android-LInux-ARM开发板?

  2. SpringBoot AOP控制Redis自动缓存和更新

    导入redis的jar包 <!-- redis --> <dependency> <groupId>org.springframework.boot</gro ...

  3. 最牛技术 1秒启动Linux的窍门

    1秒启动Linux可以实现吗?我们知道Linux系统开机并不算快,最少也需要11秒,但是,现在有一个技巧,可以1秒打开linux系统,到底是什么技术这么牛?请看下文详细介绍 尽可能快的启动系统,对于自 ...

  4. linux 终端分屏命令

    比如:某文件夹下有文件:vector.cc, substr.cc 1.使用vim命令打开任意一个文件:vim vector.cc打开第一个文件.如下图所示: 2.按:"Esc"键 ...

  5. linux service start|stop|restart

    用了这么些日子的linux/unix系统,也和别人一起合作开发了不少程序,发现高手都喜欢在命令行上操作,而且控制程序的运行偏好于使用脚本,加上参数如:start.restart.stop等. 后来自己 ...

  6. Scala- Double类型工具类

    格式化分数,按照指定小数位四舍五入工具类 package com.rz.util object NumberUtils { /** * 格式化小数 * @param num Double对象 * @p ...

  7. xlearn安装

    xlearn简介 xLearn is a high performance, easy-to-use, and scalable machine learning package, which can ...

  8. Xposed框架

    Xposed框架,很好的一款软件,早起百团大战.外卖大战时候,对拉新用户有很大的帮助,一直没时间整理,今天有看到一个公众账号介绍这款,准备大概整理下,做个记录. 整理下思路 新用户,无非就是1.手机号 ...

  9. Selenium with Python 003 - 页面元素定位

    WebUI自动化,首先需要定位页面中待操作的元素,然后进行各种事件操作,这里我们首先介绍Selenium Python 如何定位页面元素,WebDriver 提供了一系列的方法. 定位单个页面元素(返 ...

  10. cassandra框架模型之二——存储机制 CommitLog MemTable SSTable

    四.副本存储 Cassandra不像HBase是基于HDFS的分布式存储,它的数据是存在每个节点的本地文件系统中. Cassandra有三种副本配置策略: 1) SimpleStrategy (Rac ...