[勇者闯LeetCode] 112. Path Sum

Description

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, return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

              5/ \
            4   8/   / \
          11  13  4/  \      \
        7    2      1

Information

  • Tags: Tree | Depth-first Search
  • Difficulty: Easy

Solution

使用深度优先搜索,遇到叶节点时判断路径的和是否与目标值相等。

Python Code

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution(object):def hasPathSum(self, root, sum):""":type root: TreeNode:type sum: int:rtype: bool"""if root is None:return Falseif root.left is None and root.right is None and root.val == sum:return Trueelse:return self.hasPathSum(root.left, sum-root.val) or\self.hasPathSum(root.right, sum-root.val)

[勇者闯LeetCode] 112. Path Sum相关推荐

  1. [勇者闯LeetCode] 1. Two Sum

    [勇者闯LeetCode] 1. Two Sum Description Given an array of integers, return indices of the two numbers s ...

  2. leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)

    题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...

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

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

  5. [勇者闯LeetCode] 83. Remove Duplicates from Sorted List

    [勇者闯LeetCode] 83. Remove Duplicates from Sorted List Description Given a sorted linked list, delete ...

  6. [勇者闯LeetCode] 6. ZigZag Conversion

    [勇者闯LeetCode] 6. ZigZag Conversion Description The string "PAYPALISHIRING" is written in a ...

  7. [勇者闯LeetCode] 70. Climbing Stairs

    [勇者闯LeetCode] 70. Climbing Stairs Description You are climbing a stair case. It takes n steps to rea ...

  8. [勇者闯LeetCode] 191. Number of 1 Bits

    [勇者闯LeetCode] 191. Number of 1 Bits Description Write a function that takes an unsigned integer and ...

  9. [勇者闯LeetCode] 38. Count and Say

    [勇者闯LeetCode] 38. Count and Say Description The count-and-say sequence is the sequence of integers b ...

最新文章

  1. 【iOS工具】rvm、Ruby环境和CocoaPods安装使用及相关报错问题解决(2016 12 15 更新)...
  2. 结构体struct 联合体 union
  3. java 从数据库取值_JAVA操作数据库--从一张表中取值,经过判断,然后插入另一张表中。...
  4. bind()、call()、apply()理解及用法
  5. ngnx 301 302跳转配置方法
  6. 社交网络登录失败 当尝试用您的社交网络账号进行第三方登录时,发生了一个错误。
  7. 常州新北区华为云_常州高新区召开大数据时代健康产业创新大会
  8. 什么是机器学习---人工智能工作笔记0012
  9. c#读取生成excel表格文件xls、xlsx格式文件
  10. 同一主机,不同域名绑定不同网站(IIS主机头实现方法)
  11. MySQL获取数据库连接对象_利用JDBC连接mysql数据库,获取连接对象的通用格式
  12. 关于SQL92标准和Sybase,SQLServer2000,Oracle的数据类型对比关系
  13. 在个人机上发布web项目
  14. 笔试算法题(17):奇偶数分置数组前后段 反序访问链表
  15. python网址编码转换_python实现中文转换url编码的方法
  16. 串的模式匹配算法 ← BF算法
  17. For 循环优化,提升效率
  18. 服务器显示屏 超出工作频率范围,WIN7电脑显示器超出工作频率范围的处理方法...
  19. eve模拟器上虚拟服务器,没有真机怎么做实验?EVE模拟器了解一下
  20. unity blend 笔记

热门文章

  1. 用计算机录制声音让音质更好,电脑如何录屏?电脑录屏如何把声音也录制下来...
  2. 3D打印技术分类浅析
  3. 软考--DNS迭代查询与递归查询
  4. python笔记34 文件操作
  5. 1分钟学会用Midjourney做自己的皮克斯风格的卡通形象
  6. 10月8日维护服务器,10月8日新开服务器活动公告
  7. 计算机网络军训口号,计算机专业军训口号
  8. 所有c语言语句最后必须有一个分号,C 语言学习笔记
  9. Esp8266 / nodeMCU / wemos D1 MINI GUIslice的配置(官翻)
  10. php 取html文本框的值,jQuery中怎么获取文本框的值