原题链接在这里: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. Tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中

    Tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中 在Linux系统中,Tomcat 启动后默认将很多信息都写入到 catalina.out 文件中,我们可以通过tail  ...

  2. linux中的信号简介和trap命令

    1.信号 linux通过信号来在运行在系统上的进程之间通信,也可以通过信号来控制shell脚本的运行 主要有一下信号 1 ##进程重新加载配置 2 ##删除进程在内存中的数据 3 ##删除鼠标在内存中 ...

  3. awk分割列-【AWK学习之旅】

    ---===AWK学习之旅===--- awk 内置分割函数:split,将列按照指定分割符,分割成数组 用法:split(str1,array,"分隔符") 文件内容: [roo ...

  4. 《棋牌游戏服务器》斗地主AI设计

    设计目标 要取得良好效果,首先要搞清楚一个问题:我们想得到一个什么样的斗地主AI?我们的AI是用在手游产品当中,在真实玩家不足时为用户提供陪玩服务,这个目标决定了这个AI要具备以下两个核心特点:1.执 ...

  5. Linux系统中使用netcat命令的奇技淫巧

    netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所做的 ...

  6. MapReduce数据筛选

    需求: 编写MapReduce程序算出高峰时间段(如9-10点)哪张表被访问的最频繁的表,以及这段时间访问这张表最多的用户,以及这个用户访问这张表的总时间开销. 测试数据: TableName(表名) ...

  7. JMS-activeMq发布订阅模式

    上一篇对点对点模式进行总结,这一篇都发布订阅模式进行总结,代码都差不多,唯一区别就是创建队(session.createQueue(目的地))列改为创建主题(session.createTopic(目 ...

  8. JMS-activeMq点对点模式

    上一篇对JMS进行介绍了一下,接下来总结一下activemq点对点模式以及订阅发布模式. (1)下载:首先到官网http://activemq.apache.org下载activemq (2)运行:解 ...

  9. TWX 比较好的多线程使用实例

    using Newtonsoft.Json;using System;using System.Collections.Generic;using System.Configuration;using ...

  10. Ubuntu 16 下面的文件比较工具 Meld

    安装 sudo apt-get install meld 使用 很好用,很方便.支持文件比较,文件夹比较.