[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 ...
随机推荐
- 经典收藏链接(C#总结)
去年底转到Java,在此总结一下.很多不错的C#博客在此收藏标记\(^o^)/~ 1.基础知识 Linq专题:http://www.cnblogs.com/RuiLei/archive/2009/09 ...
- 比较有意思的原生态js拖拽写法----摘自javascript高级程序设计3
var DragDrop = function () { var dragging = null; var diffX = 0; var diffY = 0; function handleEvent ...
- OpenGL 多线程共享纹理
1:opengl 多线程共享纹理纹理: //解码时候使用opengl进行绘制,需要构建队列和两个线程,分别用于解码数据并且填充纹理和渲染. 主线程常见两个共享上下文: main() { ⋯⋯⋯⋯ gH ...
- Erasure Coding(纠删码)深入分析
http://blog.sina.com.cn/s/blog_57f61b490102viq9.html 1.前言 Swift升级到2.0大版本后宣称开始支持纠删码,这其实是一个很有意义的特性,主要是 ...
- 使用 xhprof 进行性能分析
xhprof 是 facebook 开发的一个PHP扩展,作用来是用来做性能剖析.其官网:http://pecl.php.net/package/xhprof [安装] 1,下载最新的 t ...
- navicate for mysql mac 含注册机 亲测可用
百度网盘:https://pan.baidu.com/s/1hrXnRes
- PS前端
学习使用Photoshop的基本使用,以及Photoshop中关于切图这一块的知识,目的是能熟练使用Photoshop查看UI设计师的设计效果图,同时利用Photoshop切图来制作专业html页面. ...
- PHP获取跳转后的URL,存到数据库,设置缓存时间
<?php error_reporting(0); header("Content-Type: text/html; charset=utf-8"); $fid=$_GET[ ...
- margin-top和padding-top
padding- top 在原有的基础上进一步的扩张 margin - top 在原有的位置上发生上下的平移 <!DOCTYPE html> <html lang="en& ...
- getitem, setitem, delitem (把类实例化成字典的类型)
class Foo(object): def __init__(self): self.data = {} def __getitem__(self, key): ...