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. ps命令 百度+加自己的理解

    ps故为process status的缩写,即为进程状态的命令, ps命令详解, 1)ps a 显示现行终端机下的所有程序,包括其他用户的程序.2)ps -A 显示所有程序.3)ps c 列出程序时, ...

  2. 【读书笔记 - Effective Java】05. 避免创建不必要的对象

    1. 如果对象是不可变的(immutable),它就始终可以被重用. (1) 特别是String类型的对象. String str1 = new String("str"); // ...

  3. angular2配置使用ng2-bootstrap

    第一步,安装.进入项目目录 npm install ng2-bootstrap bootstrap --save 第二步,angular-cli 配置 ng2-bootstrap   src/.ang ...

  4. Drill-On-YARN

    1. Drill-On-YARN介绍 功能 启动 停止 扩容 缩容 failover 启动流程 下载drill的社区包,进行必要的配置,执行drill-on-yarn.sh start命令,启动dri ...

  5. 【NXP开发板应用—智能插排】3.驱动GPIO点亮外接LED

    [前言] 首先感谢深圳市米尔科技有限公司举办的这次活动并予以本人参加这次活动的机会,以往接触过嵌入式,但那都是皮毛,最多刷个系统之类的,可以说对于嵌入式系统开发这件事情是相当非常陌生的,这次活动为我提 ...

  6. python应用:selenium之爬取天眼查信息

    inform_table.py # -*-coding:utf8-*- from selenium import webdriver from selenium.webdriver.common.pr ...

  7. 关于MySQL的锁机制详解

    锁概述 MySQL的锁机制,就是数据库为了保证数据的一致性而设计的面对并发场景的一种规则. 最显著的特点是不同的存储引擎支持不同的锁机制,InnoDB支持行锁和表锁,MyISAM支持表锁. 表锁就是把 ...

  8. PTA基础编程题目集7-2然后是几点

    有时候人们用四位数字表示一个时间,比如1106表示11点零6分.现在,你的程序要根据起始时间和流逝的时间计算出终止时间. 读入两个数字,第一个数字以这样的四位数字表示当前时间,第二个数字表示分钟数,计 ...

  9. vim 智能提示插件 YouCompleteMe安装

    按照网上的教程安装该软件,没有一篇是成功的(也有可能是没找对).自己从网上东拼西凑的,终于让自己的vim智能识别了. 1. 升级 vim: (ubuntu) sudo add-apt-reposito ...

  10. 浅谈fail-fast机制

    fail-fast机制即为快速失败机制,个人认为是一种防护措施,在集合结构发生改变的时候,使尽全力抛出ConcurrentModificationException,所以该机制大部分用途都是用来检测B ...