题目地址:https://leetcode-cn.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 tree 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.

题目大意

给定一个包含三位整数的升序数组,表示一棵深度小于 5 的二叉树,请你返回从根到所有叶子结点的路径之和。

解题方法

DFS

这个题很新颖,很少见。我们知道树的表示方法有两种,一种是链表方式,一种是数组方式。这个题考的就是我们的数组方式。数组方式也是很有用的,比如在堆排序中,就很实用。

在一个数组表示的树中,如果一个节点的索引是index,那么其左孩子是index * 2,右孩子是index * 2 + 1。

我们先把给出的nums数组进行拆解,把每个数放入数组中对应的节点中。数组的默认数字是-1,也就是说-1表示空节点。

再计算带权路径和的时候,需要找到每个叶子节点的路径,判断是否是叶子节点的方法是看其两个孩子的值是不是都是-1。题目给定的数组的深度是5,那么最多有32个叶子节点,为了方便叶子节点的判断,选择了大小为64的数组。

如果一个节点是叶子节点,那么累加路径长度到最后的结果中就行了。

C++代码如下:

class Solution {
public:
int pathSum(vector<int>& nums) {
vector<int> tree(64, -1);
for (int n : nums) {
int v = n % 10; n /= 10;
int p = n % 10; n /= 10;
int d = n % 10;
tree[(int)pow(2, d - 1) + p - 1] = v;
}
dfs(tree, 1, 0);
return sum;
}
void dfs(vector<int>& tree, int index, int parent) {
if (tree[index] == -1) return;
if (tree[index * 2] == -1 && tree[index * 2 + 1] == -1) {
sum += parent + tree[index];
return;
}
dfs(tree, index * 2, parent + tree[index]);
dfs(tree, index * 2 + 1, parent + tree[index]);
}
private:
int sum = 0;
};

日期

2019 年 9 月 24 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪

【LeetCode】666. Path Sum IV 解题报告 (C++)的更多相关文章

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

  2. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

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

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

  5. 【LeetCode】437. Path Sum III 解题报告(Python)

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

  6. 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)

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

  7. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

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

  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. Windows下的Python多版本管理?

    虽然接触了好几年python,但一些细节没有注意.最近看网课,看到这个Windows系统下Python多版本管理的问题,记录下备忘. 假设现在windows环境中有python2,python3和an ...

  2. memset初始化值的效率秒杀for循环

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  3. TensorFlow 2.0 深度学习实战 —— 浅谈卷积神经网络 CNN

    前言 上一章为大家介绍过深度学习的基础和多层感知机 MLP 的应用,本章开始将深入讲解卷积神经网络的实用场景.卷积神经网络 CNN(Convolutional Neural Networks,Conv ...

  4. Hadoop入门 概念

    Hadoop是分布式系统基础架构,通常指Hadoop生态圈 主要解决 1.海量数据的存储 2.海量数据的分析计算 优势 高可靠性:Hadoop底层维护多个数据副本,即使Hadoop某个计算元素或存储出 ...

  5. Android系统编程入门系列之硬件交互——多媒体麦克风

    在多媒体摄像头及相关硬件文章中,对摄像头的使用方式需要区分应用程序的目标版本以使用不同的代码流程,而与之相比,麦克风硬件的使用就简单多了. 麦克风及相关硬件 麦克风硬件在移动设备上作为音频的采集设备, ...

  6. A Child's History of England.21

    There was one tall Norman Knight who rode before the Norman army on a prancing horse, throwing up hi ...

  7. C++之无子数

    题目如下: 1 #include <iostream> 2 3 using namespace std; 4 5 6 bool isThisNumhaveChild(int num); 7 ...

  8. android 下动态获取控件的id

    有时候我们需要动态的取得一个一个控件的id,然后进行操作,经过在网上查找,找到了一下方法getResources().getIdentifier("textView01", &qu ...

  9. java网站架构设计

    涉及到的技术及工具:java,springmvc,ibatis,freemarker,mysql,mongdb,memcached,ehcache,maven. 一个网站不可能说一开始就是要设计一个能 ...

  10. linux系统下安装dubbo-admin

    1.在安装dubbo-admin之前确保你得linux服务器上已经成功安装了jdk,tomcat, 若还没安装jdk以及tomcat则参考我的上一篇文章"linux环境下安装jdk,tomc ...