112 Path Sum 路径总和
给定一棵二叉树和一个总和,确定该树中是否存在根到叶的路径,这条路径的所有值相加等于给定的总和。
例如:
给定下面的二叉树和 总和 = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
返回 true, 因为存在总和为 22 的根到叶的路径 5->4->11->2。
详见:https://leetcode.com/problems/path-sum/description/
Java实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root==null){
return false;
}else if(root.left==null&&root.right==null&&root.val==sum){
return true;
}
return (hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val));
}
}
112 Path Sum 路径总和的更多相关文章
- 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路径和(是否有路径)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【LeetCode】Path Sum(路径总和)
这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...
- 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 ...
- 112. Path Sum二叉树路径和
[抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
随机推荐
- ios非UTF-8格式的网页解析
网上有很多关于ios xml解析的方法,关于非UTF-8格式的网页解析也不少,我也试着看了好几个,但都没成功.今天无意中却弄好了,所以想和大家分享下.其实很简单,下面说下怎么得到非UTF-8格式的网页 ...
- BestCoder6 1002 Goffi and Squary Partition(hdu 4982) 解题报告
题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=530 (格式有一点点问题,直接粘 ...
- Session移除
Session.Clear()就是把Session对象中的所有项目都删除了,Session对象里面啥都没有.但是Session对象还保留. Session.Abandon()就是把当前Session对 ...
- java运行Linux命令
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="UT ...
- LA-5059(组合游戏)
题意: 有n堆石子,分别有a1,a2,...,an个,两个游戏者轮流操作,每次可以选一堆m拿走至少一个且不超过一半的石子,谁不能拿石子就算输; 思路: a1太大打印sg表找规律,然后就是异或和了; A ...
- hdu-5750 Dertouzos(数论)
题目链接: Dertouzos Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- 兼容html5新标签及媒体查询引入插件
<!-- 以下2个插件是用于在IE8支持HTML5元素和媒体查询的,如果不用可移除 --> <!--[if lt IE 9]> <script src="htt ...
- 光流 LK 金字塔
文章转载自:https://blog.csdn.net/sgfmby1994/article/details/68489944 光流是图像亮度的运动信息描述,这种运动模式指的是由一个观察者(比如摄像头 ...
- Microsoft Speech SDK开发包 使用
下载开发包.我们首先从微软的官网上面下载开发包,下载地址如下: http://www.microsoft.com/en-us/download/details.aspx?id=10121我们主要下载三 ...
- fragment error
error: android.view.InflateException: Binary XML file line #6: Error inflating class fragment 解决办法 ...