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.

思路:

用一个map -- flag记录二叉树是否到了叶节点。用另一个map记录树节点所在位置和对应的值。

先序遍历二叉树,如果到了根节点,将当前路径和pathsum加到返回值总的和ret中。

遍历左子树。

遍历右子树。

void getsum(int level, int pos, map<int, int>& mp, map<int, bool>& flag, int pathsum, int& ret)
{
if (level >= || !flag[level * + pos])return ;//结点不存在
pathsum += mp[level * + pos];//当前路径和
if (!flag[(level + ) * + pos * ] && !flag[(level + ) * + pos * - ])ret += pathsum ;//到了叶节点 getsum(level+,pos*-,mp,flag,pathsum,ret);
getsum(level+,pos*,mp,flag,pathsum,ret);
}
int pathSum(vector<int>& nums)
{
map<int, int>mp;
map<int, bool>flag;
int ret=;
if (nums.size() == ) return ;
if (nums.size() == )return nums[] % ; for (auto n : nums){ mp[n / ] = n % ; flag[n / ] = true; } getsum(, , mp, flag, , ret);
return ret;
}

[leetcode-666-Path Sum IV]的更多相关文章

  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】666. Path Sum IV 解题报告 (C++)

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

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

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

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

  6. [LeetCode] 437. Path Sum III_ Easy tag: DFS

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

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

  8. [LeetCode] 113. Path Sum II 路径和 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] 437. Path Sum III 路径和 III

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

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

随机推荐

  1. 国产Linux下开发正式开工(deepin)

    配置开发环境 1.一般工具 在深度商店安装QQ,微信,安装一般软件WPS,Navicat数据库工具,文本编辑notepadqq. 影视娱乐爱奇艺,优酷,酷狗. 2.安装主要的开发环境 (1)c# 深度 ...

  2. JavaScript数组处理方法

    JavaScript中创建数组有两种方式 (一)使用 Array 构造函数: var arr1 = new Array(); //创建一个空数组 var arr2 = new Array(20); / ...

  3. meven 配置

    配置meven 自定义文件[User Settings] D:\java\apache-maven-3.5.3\conf\settings.xml settings.xml 新增两端配置信息 < ...

  4. 开发和调试第一个 LLVM Pass

    1. 下载和编译 LLVM LLVM 下载地址 http://releases.llvm.org/download.html,目前最新版是 6.0.0,下载完成之后,执行 tar 解压 llvm 包: ...

  5. 【C】三目运算符(先是问号之后又是冒号的那个)

    // 看这个例子就可以懂了 a = b == c ? d : e ; //如果 b==c,执行 a=d //否则执行 a=e //为了方便阅读,也可以改成下方代码 a = (b == c) ? d : ...

  6. opencv3 学习五 - 合并与分割通道

    合并与分割通道 程序如下 #include "opencv2/opencv.hpp" using namespace cv; int main() { Mat original = ...

  7. js分片上传大文件,前端代码

    首先导入jQuery.form.js文件,下面src是相对于改js文件位置, <script type="text/JavaScript" src="jquery/ ...

  8. react路由按需加载方法

    使用router4之后以前的按需加载方法require.ensure 是不好使了. 所以我们改用react-loadable插件做按需加载. 第一步: yarn add react-loadable ...

  9. 状压搜索 洛谷T47092 作业

    TYM 有 nn 本作业,编号为 1,\dots,n1,…,n. 由于 \mathrm{TYM}TYM 很喜欢偷懒,而且不喜欢消耗脑细胞,所以他选择跳着完成这 nn 本作业.此外,如果将做作业的顺序转 ...

  10. 北京Uber优步司机奖励政策(1月9日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...