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.

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

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

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

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

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

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

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

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

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

  9. (二叉树 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 ...

随机推荐

  1. 封装jQuery Validate扩展验证方法

    一.封装自定义验证方法-validate-methods.js /***************************************************************** j ...

  2. javascript中的链表结构—从链表中删除元素

    1.概念 上一个博文我们讲到链表,其中有一个方法remove()是暂时注释的,这个方法有点复杂,需要添加一个Previous()方法找到要删除的元素的前一个节点,这一个博文我们来分析一下这个remov ...

  3. SQL Server之游标的基础知识

    什么是游标: 游标是可以在结果集中上下游动的指针. 游标的作用: --允许定位到结果集中的特定行. --从结果集的当前位置检索一行或多行数据. --支持对结果集中当前位置的行进行修改. 注意:游标虽然 ...

  4. 经典算法和OJ网站(开发者必备-转)

    一. Online Judge简介: Online Judge系统(简称OJ)是一个在线的判题系统.用户可以在线提交程序多种程序(如C.C++.Pascal)源代码,系统对源代码进行编译和执行,并通过 ...

  5. 柯里化/偏函数/Curring用法

    把接受多个参数的函数变成一个单一参数的函数,并且返回接受余下的参数而有返回结果的新函数的技术 下面我们以实例说明: var toString = {}.toString; var isString = ...

  6. FineUI大版本升级,外置ExtJS库、去AXD化、表格合计行、表格可编辑单元格的增删改、顶部菜单框架

    这是一篇很长的文章,在开始正文之前,请允许我代表目前排名前 20 中唯一的 .Net 开源软件 FineUI 拉下选票: 投票地址: https://code.csdn.net/2013OSSurve ...

  7. lecture1-NN的简介

    这是DL的发明人Hinton在多伦多大学的2013年冬季教授de课程,并将视频分享到coursera网站上.其中不但有视频,也有课件,但是Hinton主页上还有他上课的课后问题,Hinton告诉学生这 ...

  8. Python2.3-原理之语句和语法

    此节来自于<Python学习手册第四版>第三部分 一.python语句简介(第10章) 1.首先记得一个概念:a.程序由模块构成:b.模块包含语句:c.语句包含表达式:d.表达式建立并处理 ...

  9. 读懂IL代码就这么简单 (一)

    一前言 感谢 @冰麟轻武 指出文章的错误之处,现已更正 对于IL代码没了解之前总感觉很神奇,初一看完全不知所云,只听高手们说,了解IL代码你能更加清楚的知道你的代码是如何运行相互调用的,此言一出不明觉 ...

  10. TensorFlow 源代码初读感受

    把自己微博发的文章:http://www.weibo.com/1804230372/En7PdlgLb?from=page_1005051804230372_profile&wvr=6& ...