[leetcode-666-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:
- The hundreds digit represents the depth
Dof this node,1 <= D <= 4. - The tens digit represents the position
Pof this node in the level it belongs to,1 <= P <= 8. The position is the same as that in a full binary tree. - The units digit represents the value
Vof 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]的更多相关文章
- [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 ...
- 【LeetCode】666. Path Sum IV 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 【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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- mysql 一看就会 基本语法
创建表 create table <表名>( <字段名> 类型(长度) not null primary key auto_increment, **主键 name char ...
- TCP/IP协议族之链路层(二)
TCP/IP学习记录,如有错误请指正,谢谢!!! TCP/IP协议族之链路层(二) 链路层是最底层协议,主要有三个目的: 1. 为IP模块发送和接收IP数据报 2. 为ARP模块发送ARP请求和接收A ...
- border-radius__边框圆角
1.四个参数: border-radius: 值1 值2 值3 值4;顺序:从左开始,顺时针顺 div{ width: 200px; height: 100px; background-color: ...
- 微信小程序 唯一标识 加减
var nums = 'goods_list[' + e.currentTarget.dataset.indexs+'].goods_num' //console.log(nuns) var num ...
- Mac下PHP的环境搭建
* 前段时间手欠 ... 入手了一个二手的Macbook pro ! 配置挺高的 16款13寸的基本顶配了 ... 只差 硬盘不是1T的 ... 可以脑补一下配置了* 话说 不是所有程序猿都说 每个程 ...
- asp.net在一般处理程序里面操作Session
1.要在一般处理程序中获取其他页面的session值,需要引用名空间: using System.Web.SessionState; 2.然后继承一个接口:IRequiresSessionState ...
- HaoheDI让ETL变得简单
HaoheDI(昊合数据整合平台)http://www.haohedi.com,产品基于BS架构,开发运维均极为简单,可快速搭建ETL平台,广泛支持各种数据库.文本文件.SAP和Hadoop,开发数据 ...
- C语言之一般树
1.一般树 将这种一般的树转化成我们熟悉的单链表形式,这有三层,每一层都可以看成单链表或者多个分散的单链表 数据节点如下: struct tree { int elem; ...
- Kylin 几个sql报错原因 汇总
Can't create EnumerableAggregate! while executing SQL由distinct count引起的错误 null while executing SQLjo ...
- Prim算法堆优化
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> ...