【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-digits integers. For each integer in this list:
The hundreds digit represents the depth D of this node, 1 <= D <= 4.
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.
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.
这个题目比较直接的方法是把二叉树建立起来,然后从根节点开始依次遍历各个叶子节点。这个方案会比较麻烦,因为涉及到回溯等等。更为简便的方法是从叶子节点往根节点遍历,把每个叶子节点遍历到根节点的和累加就行。
1. 找出所有的叶子节点。这个比较简单,遍历nums数组的每个元素,假设元素的值是abc,那么只要判断(a+1)(b*2)*或者(a+1)(b*2-1)*在不在nums数组中即可。【b*2中的*表示乘号,括号后面的*表示通配符】
def IsLeaf(self,nums,node):
dep = node/100
pos = (node%100)/10 next = (dep+1)*10 + pos*2
next2 = (dep+1)*10 + pos*2 -1
for i in nums:
if i/10 == next or i/10 == next2:
return False
return True
2. 找出叶子节点的父节点。这个同理,假设叶子节点的值是假设元素的值是abc,那么其父节点是(a-1)(b/2)*或者是(a-1)((b+1)/2)*。
def getParent(self,nums,node):
dep = node/100
pos = (node%100)/10
val = (node%100)%10
for i in nums:
if i/100 != dep -1:
continue
p = (i%100)/10
if p*2 == pos or p*2 == pos+1:
self.count += (i%100)%10
return i
return node
3.接下来就是开始求和了。
完整代码如下:
class Solution(object):
count = 0
def getParent(self,nums,node):
dep = node/100
pos = (node%100)/10
val = (node%100)%10for i in nums:
if i/100 != dep -1:
continue
p = (i%100)/10
if p*2 == pos or p*2 == pos+1:
self.count += (i%100)%10
return i
return node
def IsLeaf(self,nums,node):
dep = node/100
pos = (node%100)/10 next = (dep+1)*10 + pos*2
next2 = (dep+1)*10 + pos*2 -1
for i in nums:
if i/10 == next or i/10 == next2:
return False
return True def pathSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maxDep = 0
for i in nums:
if maxDep < i /100:
maxDep = i/100
for i in nums:
if self.IsLeaf(nums,i) == False:
continue
else:
self.count += (i%100)%10
while True:
i = self.getParent(nums,i)
if i /100 == 1:
break return self.count
【leetcode】Path Sum IV的更多相关文章
- 【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 ...
- 【LeetCode】Path Sum 2 --java 二叉数 深度遍历,保存路径
在Path SUm 1中(http://www.cnblogs.com/hitkb/p/4242822.html) 我们采用栈的形式保存路径,每当找到符合的叶子节点,就将栈内元素输出.注意存在多条路径 ...
- 【LeetCode】Path Sum ---------LeetCode java 小结
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- 【leetcode】Path Sum
题目简述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- 【leetcode】Path Sum I & II(middle)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【LeetCode】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】Path Sum(路径总和)
这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- [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 ...
随机推荐
- Dynamic Web TWAIN——网页扫描SDK
下载地址:[https://www.dynamsoft.com/Support/DWTGuide/Dynamic%20Web%20TWAIN%20SDK.html] API:[http://devel ...
- Gin框架中文文档
Gin 是一个 go 写的 web 框架,具有高性能的优点.官方地址:https://github.com/gin-gonic/gin 带目录请移步 http://xf.shuangdeyu.com/ ...
- Nginx Server 上80,443端口。http,https共存
server{ listen 80; listen 443 ssl; server_name www.iamle.com; index index.html index.htm index.php; ...
- 【HANA系列】SAP Vora(SAP HANA和Hadoop)简析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP Vora(SAP HAN ...
- kafka语句示例
1.从http://kafka.apache.org/下载kafka安装包:2.tar zxvf kafka_2.8.0.tar.gz,修改配置文件conf/server.properties:bro ...
- [转帖]Ubuntu 18.04 server安装图形界面及realvnc远程桌面连接
Ubuntu 18.04 server安装图形界面及realvnc远程桌面连接 https://blog.csdn.net/networken/article/details/88938304 转帖 ...
- mybatis字段映射枚举类型
在底层使用mybatis的时候,我们可能会需要把表里的字段映射到Java里面的枚举类,现总结下工作中的用法: sku表里一个status_type字段为int类型.(这里是postgresql的脚本) ...
- 洛谷 U78696 图书馆馆长的考验 题解
题面 1. 图书馆馆长的考验(library) 红魔馆的拥有者蕾米莉亚的好友帕秋莉是红魔馆的大图书馆的馆长.擅长操纵五行,名言是“万物都有属性.所谓的属性,和弱点是一样的”. 一天,因为魔理沙看了神之 ...
- 树型DP入门
题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚 ...
- ps -ef 和ps -aux的区别
在 linux 显示进程的命令是ps ,常用的是 ps -ef,今天看到了还有一个ps -aux,查询了资料,这里总结一下 那么ps -ef 和ps -aux 有什么区别呢? 其实区别不是很大,这就要 ...