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 the values along the path equals the given sum.
Given the below binary tree and
sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
解法: 使用递归。在例子中,存在一个从root出发的,sum=22的路径 <=> 存在从4出发的,sum=22-4的路径 or 存在从8出发的,sum=22-8的路径
class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
""" if not root:
return False
elif root.val == sum and not root.left and not root.right:
return True
else:
return self.hasPathSum(root.right, sum - root.val) or self.hasPathSum(root.left, sum - root.val)
也可以使用DFS+backtracking
class Solution(object):
def hasPathSum(self, root, s):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if not root:
return False res = []
self.dfs(root, s, [root.val], res)
return any(res) def dfs(self, root, s, path, res): if not root.left and not root.right and sum(path) == s:
res.append(True) if root.right:
self.dfs(root.right, s, path+[root.right.val], res)
if root.left:
self.dfs(root.left, s, path+[root.left.val], res)
Leetcode 112. Path Sum的更多相关文章
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- [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] 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] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- 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 112 Path Sum ----- java
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Java [Leetcode 112]Path Sum
题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- [Leetcode]112. Path Sum -David_Lin
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- (二叉树 DFS 递归) 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 ...
随机推荐
- java工程中的.classpathaaaaaaaaaaaaaaaa<转载>
第一部分:classpath是系统的环境变量,就是说JVM加载类的时候要按这个路径下去找,当然这个路径下可以有jar包,那么就是jar包里所有的class. eclipse build path是ec ...
- codevs2806 红与黑
难度等级:白银 codevs2806 红与黑 题目描述 Description 有一个矩形房间,覆盖正方形瓷砖.每块瓷砖涂成了红色或黑色.一名男子站在黑色的瓷砖上,由此出发,可以移到四个相邻瓷砖之一, ...
- U3D杂记
1,点击UI上的登录按钮,从脚本发出ioo.netmanager.SendConnet->进入CS->soketclient.sendconnet...->netmanager调用 ...
- 利用ThinkPHP自带的七牛云驱动上传文件到七牛云以及删除七牛云文件方法
一.准备工作 1.注册七牛云账号 2.选择对象储存->创建空间->设置为公开 3.在config配置文件中添加以下代码 'UPLOAD_FILE_QINIU' => array ( ...
- 安装mint的时候提示:Not compatible with your operating system or architecture: fsevents@1.0.11
Since fsevents is an API in OS X allows applications to register for notifications of changes to a g ...
- 实验三 敏捷开发与XP实践
实验内容 1. XP基础 2. XP核心实践 3. 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2.完成实验 ...
- 文件夹管理工具(MVC+zTree+layer)(附源码)
写在前 之前写了一篇关于 文件夹与文件的操作的文章 操作文件方法简单总结(File,Directory,StreamReader,StreamWrite ) 把常用的对于文件与文件夹的操作总结了一 ...
- CMD命令简单使用
1.定位磁盘 2.打开文件路径 3.查看文件目录里面所有的文件或目录信息
- MVC———用自定义扩展类实现验证
废话少说,直接上图 →_→ NO.1 NO.2 NO.3 NO.4 NO.5 NO.6 NO.7 NO.8 NO.9 NO.10 NO.11 NO.12 NO.13 NO.14 NO.15 NO.16 ...
- BroadcastReceiver之SD的挂载监听
首先,新建一个类,继承于BroadcastReceiver,然后去配置Manifest.xml这就不用说了, 注意配置Manifest.xml时候的一些细节 必须加上<data android: ...