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.

Note: A leaf is a node with no children.

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.

典型的DFS, 然后需要将目前的path sum一起append进入stack里面, 然后判断path sum 是否跟给的sum相等并且是leaf, 即可.

1. Constraints

1) can be empty

2. IDeas

DFS:     T:O(n)    S: T(n)

3. Code

3.1) iterable

 # iterable

 class Solution:
def pathSum(self, root, s):
if not root: return False
stack =[(root, root.val)]
while stack:
node, ps = stack.pop()
if ps == s and not node.left and not node.right:
return True
if node.left:
stack.append((node.left, ps + node.left.val))
if node.right:
stack.append((node.right, ps + node.right.val))
return False

3.2) recursive way.

 class Solution:
def pathSum(self, root, s):
if not root: return False
if s- root.val == 0 and not root.left and not root.right:
return True
return self.pathSum(root.left, s- root.val) or self.pathSum(root.right, s- root.val)

4. Test cases

1) edge case

2)   s= 9

      5
/ \
4 8

[LeetCode] 112. Path Sum_Easy tag: DFS的更多相关文章

  1. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  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 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

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

  5. [LeetCode] 339. Nested List Weight Sum_Easy tag:DFS

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

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

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

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

  9. LeetCode 112. Path Sum 二叉树的路径和 C++

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

随机推荐

  1. css笔记 - 张鑫旭css课程笔记之 relative 篇

    relative地址 relative 对 absolute的限制作用 限制left/top/right/bottom(方位值)定位 限制描述:absolute设置了方位值时,这些方位值是相对于pos ...

  2. OpenGL ES 系列教程

    http://www.linuxgraphics.cn/graphics/opengles_tutorial_index.html 本文收集了一套 OpenGL ES 系列教程. www.play3d ...

  3. 如何验证 Email 地址:SMTP 协议入门教程

    http://www.ruanyifeng.com/blog/2017/06/smtp-protocol.html 作者: 阮一峰 日期: 2017年6月25日   Email 是最常用的用户识别手段 ...

  4. 题目1091:棋盘游戏(DFS)

    题目链接:http://ac.jobdu.com/problem.php?pid=1091 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  5. web前端面试题(一)

    1  选择题 1.1   默认情况下,使用P标记会形成什么效果() A.在文字P所在位置中加入8个空格 B.P后面的文字会变成粗体 C.开始新的一行 D.P后面的文字会变成斜体 答案: C 1.2   ...

  6. Unity3D Shader描边效果

    Shader "Custom/RimColor" { Properties { _MainTex ("Base (RGB)", 2D) = "whit ...

  7. 23种设计模式之解释器模式(Interpreter)

    解释器模式属于类的行为型模式,描述了如何为语言定义一个文法,如何在该语言中表示一个句子,以及如何解释这些句子,这里的“语言”是使用规定格式和语法的代码.解释器模式主要用在编译器中,在应用系统开发中很少 ...

  8. 解决远程登陆Linux误按ctrl+s锁屏

      很多刚从windows转移到linux上来工作的朋友,在用vi/vim编辑文件时,常常会习惯性的按下Ctrl+s来保存文件内容.殊不知这样按下去后面会造成整个终端不响应了,ssh连接还好说,直接关 ...

  9. 企业案例(一):由于mysql sleep线程过多小故障

    1.什么是长连接 长连接是相对于通常的短连接而说的,也就是长时间保持客户端与服务端的连接状态. 通常的短连接操作步骤是: 连接->数据传输->关闭连接: 而长连接通常就是: 连接-> ...

  10. HOJ 1444 Humble Numbers

    Humble Numbers My Tags (Edit) Source : University of Ulm Internal Contest 1996 Time limit : 1 sec Me ...