题目地址: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 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪

最新文章

  1. sqlserver 游标
  2. IPC---信号量
  3. mac/linux常用命令
  4. TS 流的解码过程(系摘抄)
  5. Spring4 MVC文件下载实例
  6. Java高级之类结构的认识
  7. 293. Flip Game
  8. innodb 悲观锁,乐观锁
  9. Android两个控件叠在一起,如何让被挡住的控件显示出来
  10. 【转】[慢查优化]联表查询注意谁是驱动表 &amp; 你搞不清楚谁join谁更好时请放手让mysql自行判定
  11. 手机自动化测试:appium源码分析之bootstrap十
  12. 【WebApi系列】浅谈HTTP
  13. 基于 Redis 的分布式锁
  14. ExtJs写本地ArrayStore,ComboBox调用
  15. Centos6.5---samba文件共享服务配置(一)
  16. poj 2406 求字符串中重复子串的个数
  17. 【VSCode】Windows下VSCode便携式c/c++环境
  18. db2 v9.7 新特性cur_commit 能够实现未提交读新特性cur_commit 能够实现未提交读
  19. 003-JSR303校验
  20. ABAP-折叠窗口

热门文章

  1. 【软连接已存在,如何覆盖】ln: failed to create symbolic link ‘file.txt’: File exists
  2. Oracle-oracle中union和union all的区别
  3. JAVA中null,&quot;&quot;,equals,==相互之间使用详解
  4. 浅讲.Net 6 之 WebApplicationBuilder
  5. A Child&#39;s History of England.15
  6. 17. yum
  7. Hive(十三)【Hive on Spark 部署搭建】
  8. 【leetcode】797. All Paths From Source to Target
  9. jquery的each和js原生for循环性能对比
  10. 访问者模式(Visitor Pattern)——操作复杂对象结构