[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 ...
随机推荐
- VMware中,该如何理解桥接网络与NAT 网络模式
原创 2016年11月16日 23:26:34,原文地址如下: http://blog.csdn.net/u010801439/article/details/53193113 首先,我在VMware ...
- Java中的volatile关键字为什么不是不具有原子性
Java中long赋值不是原子操作,因为先写32位,再写后32位,分两步操作,而AtomicLong赋值是原子操作,为什么?为什么volatile能替代简单的锁,却不能保证原子性?这里面涉及volat ...
- skopt学习之路1-函数介绍:dummy_minimize
def dummy_minimize(func,dimensions,n_calls=100, x0=None, y0=None, random_state=None, verbose=False, ...
- php localeconv() 函数实例讲解
php localeconv() 函数返回一包含本地数字及货币格式信息的数组.本文章向码农介绍php localeconv() 函数的使用方法和基本实例.需要的码农可以参考一下. 定义和用法 loca ...
- python并发编程之多进程理论部分
原文连接:http://www.cnblogs.com/linhaifeng/articles/7430066.html#_label4 一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责 ...
- TensorFlow相关的一些技巧
谷歌开发技术推广工程师 Laurence Moroney 在 Google Cloud Next 大会上进行了一段 42 分钟的演讲,主题是「What's New with TensorFlow?」. ...
- SpringBoot入门篇--读取资源文件配置
在项目的开发中,我们知道的是SpringBoot框架大大减少了我们的配置文件,但是还是留下了一个application.properties文件让我们可以进行一些配置.当然这些配置必然是包括服务器的配 ...
- windows 和 Linux 安装rabbitmq
windows 安装 rabbitmq 1,安装erlang 点击进入官网下载:http://erlang.org/download/ 2.安装rabbitmq 点击进入官网下载:http://www ...
- Flutter,最好的跨平台开发框架
今天说说使用flutter的一些体会 对于Flutter,从发现到接触再到使用,不知不觉,已经有大半年了!在这段时间里,谷歌几乎每天都会更新Flutter,有时甚至一天更新几次,这让我对它更加充满信心 ...
- 1. java获取本周日-本周六的时间
Calendar calendar = Calendar.getInstance(); String[] arrDate = new String[5]; String[] arrWeek = new ...