[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.
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
代码:
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false;
if(root.left== null && root.right== null)
return root.val == sum;
if(hasPathSum(root.left, sum - root.val))
return true;
if(hasPathSum(root.right, sum - root.val))
return true;
return false;
}
}
[leetcode]112. Path Sum路径和(是否有路径)的更多相关文章
- [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 、 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 ☆(二叉树是否有一条路径的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路径总和 (C++)
题目: 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 all ...
- 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 ...
- LeetCode 112 Path Sum(路径和)(BT、DP)(*)
翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...
- LeetCode 112. Path Sum(路径和是否可为sum)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- CentOs7安装gitlab(转!)
沧浪之水清兮,可以濯吾缨; 沧浪之水浊兮,可以濯吾足. ...
- storm的代码实现
先模拟产生一些数据 我把这些数据摘一部分下来 2017-06-10 18:25:56,092 [main] [org.apache.kafka.common.utils.AppInfoParser] ...
- 生存分析与R
生存分析与R 2018年05月19日 19:55:06 走在码农路上的医学狗 阅读数:4399更多 个人分类: R语言 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- tcpdump抓sql语句
-A -n -i any |grep --color 'system_type' -n2 -- -E..,.@.@.f........ ...Ndh-....GP..:A.............. ...
- python3中的mysql数据库操作
软硬件环境 OS X EI Capitan Python 3.5.1 mysql 5.6 前言 在开发中经常涉及到数据库的使用,而python对于数据库也有多种解决方法.本文以python3中的mys ...
- python3一个简单的网页抓取
都是学PYTHON.怎么学都是学,按照基础学也好,按照例子增加印象也好,反正都是学 import urllib import urllib.request data={} data['word']=' ...
- memcache.so的报错信息,未解决
memcache.so php版本5.6 executor_globals_id in Unknown on line 0 编译也成功了,路径也是在其他so文件的目录 但是加载失败的,查看apache ...
- 比较完整的URL验证
转自:http://wuchaorang.2008.blog.163.com/blog/static/4889185220135279223253/ function IsURL(str_url){v ...
- FMX ScrollBox 拖拽控制
Firemonkey下的ScrollBox 拖拽控制,滚动控制,拖拽,滚动条 AniCalculations 仅允许纵向拖拽,拖拽 scrlbx.AniCalculations.TouchTracki ...
- 可视化库-seaborn-热力图(第五天)
1. 画一个基本的热力图, 通过热力图用来观察样本的分布情况 import matplotlib.pyplot as plt import numpy as np np.random.seed(0) ...