[勇者闯LeetCode] 112. Path Sum
[勇者闯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 andsum = 22
, return true, as there exist a root-to-leaf path5->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相关推荐
- [勇者闯LeetCode] 1. Two Sum
[勇者闯LeetCode] 1. Two Sum Description Given an array of integers, return indices of the two numbers s ...
- 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/ 题解 简单的遍历二叉树,不解 ...
- 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 ...
- 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 ...
- [勇者闯LeetCode] 83. Remove Duplicates from Sorted List
[勇者闯LeetCode] 83. Remove Duplicates from Sorted List Description Given a sorted linked list, delete ...
- [勇者闯LeetCode] 6. ZigZag Conversion
[勇者闯LeetCode] 6. ZigZag Conversion Description The string "PAYPALISHIRING" is written in a ...
- [勇者闯LeetCode] 70. Climbing Stairs
[勇者闯LeetCode] 70. Climbing Stairs Description You are climbing a stair case. It takes n steps to rea ...
- [勇者闯LeetCode] 191. Number of 1 Bits
[勇者闯LeetCode] 191. Number of 1 Bits Description Write a function that takes an unsigned integer and ...
- [勇者闯LeetCode] 38. Count and Say
[勇者闯LeetCode] 38. Count and Say Description The count-and-say sequence is the sequence of integers b ...
最新文章
- 【iOS工具】rvm、Ruby环境和CocoaPods安装使用及相关报错问题解决(2016 12 15 更新)...
- 结构体struct 联合体 union
- java 从数据库取值_JAVA操作数据库--从一张表中取值,经过判断,然后插入另一张表中。...
- bind()、call()、apply()理解及用法
- ngnx 301 302跳转配置方法
- 社交网络登录失败 当尝试用您的社交网络账号进行第三方登录时,发生了一个错误。
- 常州新北区华为云_常州高新区召开大数据时代健康产业创新大会
- 什么是机器学习---人工智能工作笔记0012
- c#读取生成excel表格文件xls、xlsx格式文件
- 同一主机,不同域名绑定不同网站(IIS主机头实现方法)
- MySQL获取数据库连接对象_利用JDBC连接mysql数据库,获取连接对象的通用格式
- 关于SQL92标准和Sybase,SQLServer2000,Oracle的数据类型对比关系
- 在个人机上发布web项目
- 笔试算法题(17):奇偶数分置数组前后段 反序访问链表
- python网址编码转换_python实现中文转换url编码的方法
- 串的模式匹配算法 ← BF算法
- For 循环优化,提升效率
- 服务器显示屏 超出工作频率范围,WIN7电脑显示器超出工作频率范围的处理方法...
- eve模拟器上虚拟服务器,没有真机怎么做实验?EVE模拟器了解一下
- unity blend 笔记
热门文章
- 用计算机录制声音让音质更好,电脑如何录屏?电脑录屏如何把声音也录制下来...
- 3D打印技术分类浅析
- 软考--DNS迭代查询与递归查询
- python笔记34 文件操作
- 1分钟学会用Midjourney做自己的皮克斯风格的卡通形象
- 10月8日维护服务器,10月8日新开服务器活动公告
- 计算机网络军训口号,计算机专业军训口号
- 所有c语言语句最后必须有一个分号,C 语言学习笔记
- Esp8266 / nodeMCU / wemos D1 MINI GUIslice的配置(官翻)
- php 取html文本框的值,jQuery中怎么获取文本框的值