【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 ...
随机推荐
- 通过NGINX location实现一个域名访问多个项目
location ~ \.php$ { root /home/webroot; //此目录下有多个项目 project1 ,project2... fastcgi_pass $php_upstr ...
- 【Qt开发】Qt5.7串口开发
QT5有专门的串口类: QSerialPort:提供访问串口的功能 QSerialPortInfo:提供系统中存在的串口的信息 具体使用方法: 1.在pro文件中加入: QT += seria ...
- (转)Jenkins2.0 Pipeline 插件执行持续集成发布流程 - git -资料 - 不错的文档
1.Jenkins 2.0 的精髓是 Pipeline as Code Jenkins 2.0 的精髓是 Pipeline as Code,是帮助 Jenkins 实现 CI 到 CD 转变的重要角色 ...
- [转帖]关于Ubuntu与Debian的关系,了解!
关于Ubuntu与Debian的关系,了解! https://blog.csdn.net/guyue35/article/details/47286193 了解一下区别.. 饮水思源:Ubuntu ...
- [洛谷P4183][USACO18JAN]Cow at Large P
题目链接 Bzoj崩了之后在洛谷偶然找到的点分好题! 在暴力的角度来说,如果我们$O(n)$枚举根节点,有没有办法在$O(n)$的时间内找到答案呢? 此时如果用树形$dp$的想法,发现是可做的,因为可 ...
- python-day11(正式学习)
目录 文件高级应用 多重操作 r+t:可读,可写(文件名为a) w+t:可写可读 a+t:可追加可读 文件内指针移动及一些操作 指针移动seek(offset,whence) 寻找指针位置tell() ...
- python——元组方法及字符串方法
元组方法 Tup.count():计算元组中指定元素出现的次数 Tup.count('c') Tup.index():在元组中从左到右查找指定元素,找到第一个就返回该元素的索引值 Tup.index( ...
- windows 2008 创建域服务器问题 账户密码不符合要求
windows 2008新建域时,本地administrator账户将成域Administrator账户.无法新建域,因为本地administrator账户密码不符合要求.*解决办法:很多人都会想到在 ...
- layer.prompt绑定确认键
case 'eventkc': top.layer.prompt({ formType: , title: '修改<span style="color:red">' + ...
- 16.AutoMapper 之可查询扩展(Queryable Extensions)
https://www.jianshu.com/p/4b23e94a7825 可查询扩展(Queryable Extensions) 当在像NHibernate或者Entity Framework之类 ...