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的更多相关文章

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

  2. 【LeetCode】Path Sum 2 --java 二叉数 深度遍历,保存路径

    在Path SUm 1中(http://www.cnblogs.com/hitkb/p/4242822.html) 我们采用栈的形式保存路径,每当找到符合的叶子节点,就将栈内元素输出.注意存在多条路径 ...

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

  4. 【leetcode】Path Sum

    题目简述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

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

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

  7. 【LeetCode】Path Sum(路径总和)

    这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...

  8. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

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

随机推荐

  1. 项目连接oracle报错:listener does not currently know of SID given in connect descriptor

    今天练习用IDEA搭建了一个ssm框架,数据库是oracle,在执行mybatis-generator时,一直报错listener does not currently know of SID giv ...

  2. vs2015中将复制过来的文件夹显示目录文件

    先将文件夹和文件复制到VS程序所在的位置,点击解决方案资源管理器上的“显示所有文件”按纽,展开这个文件夹,这样你就可以看到这个文件或者文件夹了,这时,这个文件或者文件夹是虚线构成的.你右击这个文件或者 ...

  3. deepin之添加右键新建文档选项

    deepin之添加右键新建文档选项 虽然Linux系统下所有皆文件,创建各种文件很简单,也很随意,但还是有人讨厌采用先创建空文件再改文件名的方式(比如我),我还是喜欢右键新建一个相应的源文件,可是默认 ...

  4. 好问题:count(1)、count(*)、count(列)有什么区别?

    执行效果: 1.  count(1) and count(*) 当表的数据量大些时,对表作分析之后,使用count(1)还要比使用count(*)用时多了! 从执行计划来看,count(1)和coun ...

  5. Vue.js官方文档学习笔记(二)组件化应用的构建

    组件化应用的构建 组件化应用允许我们使用小型.独立和通常可复用的组件构建大型应用. Vue注册组件 Vue.component('todo-item',{template:'<li>这是个 ...

  6. PythonDay15

    第十五章装饰器_递归 今日内容 带参数装饰器 多个装饰器修饰一个函数 递归 带参数的装饰器 # 判断argv,当登录不同的网页,会有不同的装饰效果def auth(argv):   def warpp ...

  7. Ajax提交数据后,清空form表单

    按钮不同,页面相同,还需要显示的数据不同,这里会由于页面的缓存问题,导致,每次点开这个页面显示的数据相同. 这不是我们想要的.这就需要清楚表单数据了. 如下: $('#myform')[0].rese ...

  8. 34、Scrapy 知识总结

      Scrapy 知识总结   1.安装   pip install wheel pip install https://download.lfd.uci.edu/pythonlibs/q5gtlas ...

  9. vue学习之vue-resource的引入

    npm安装的命令 npm install vue-resource --save 安装完成后在main.js中导入 import  VueResource  from 'vue-resource' V ...

  10. linux格式化磁盘命令

    linux格式化磁盘命令           linux mkfs         指令:mkfs 使用权限 : 超级使用者 使用方式 : mkfs [-V] [-t fstype] [fs-opti ...