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 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.

 class Solution {
public boolean hasPathSum(TreeNode root, int sum) { if(root==null) return false; if (root.left==null && root.right==null && sum-root.val==) return true; return hasPathSum(root.left, sum-root.val) ||hasPathSum(root.right,sum-root.val); }
}

112. Path Sum (判断路径和是否等于某值)的更多相关文章

  1. 112. Path Sum二叉树路径和

    [抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

  2. LeetCode 112 Path Sum(路径和)(BT、DP)(*)

    翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...

  3. 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 ...

  4. [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 ...

  5. [LeetCode] 437. Path Sum III 路径和 III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  6. leetcode 437 Path Sum III 路径和

      相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...

  7. 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 ...

  8. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  9. [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 ...

随机推荐

  1. [Idea Fragments]2013.08.08

    # 1 今晚看到好几篇文章把golang,Node.js还有Nginx-lua拿来说事,Node.js现在自然比较熟悉,golang则有过一些了解,而Nginx-lua则少有听到. 有好事者对Node ...

  2. Tensorflow之改变tensor形状

    https://www.tensorflow.org/versions/r0.12/api_docs/python/array_ops.html#reshape 例子: # tensor 't' is ...

  3. VS2012使用正则删除空行

    1:Ctrl + H 打开快速替换窗口 2:输入:^\s\S*$\n 3:点击使用正则替换的选项

  4. 【vijos】1543 极值问题(数论+fib数)

    https://vijos.org/p/1543 好神奇的一题.. 首先我竟然忘记n可以求根求出来,sad. 然后我打了表也发现n和m是fib数.. 严格证明(鬼知道为什么这样就能对啊,能代换怎么就能 ...

  5. spark(1.1) mllib 源码分析(三)-朴素贝叶斯

    原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/4042467.html 本文主要以mllib 1.1版本为基础,分析朴素贝叶斯的基本原理与源码 一.基本原 ...

  6. VS2008 调试出现错误 "Unable to start debugging."

    之前用的好好的调试功能,今天“F5”出现了 "Unable to start debugging." 的错误: 解决办法: 打开工程属性,选择“Debugging”,看看“Debu ...

  7. Jquery实现Bootstrap树形列表

    http://bookshadow.com/weblog/2014/05/17/jquery-bootstrap-tree-list/

  8. memcache的内存管理机制

    Memcache使用了Slab Allocator的内存分配机制:按照预先规定的大小,将分配的内存分割成特定长度的块,以完全解决内存碎片问题Memcache的存储涉及到slab,page,chunk三 ...

  9. 基础知识《十四》Java异常的栈轨迹fillInStackTrace和printStackTrace的用法

    本文转自wawlian 捕获到异常时,往往需要进行一些处理.比较简单直接的方式就是打印异常栈轨迹Stack Trace.说起栈轨迹,可能很多人和我一样,第一反应就是printStackTrace()方 ...

  10. Appcompat实现Action Bar的兼容性处理

    Appcompat实现Action Bar时,如果使用到split action bar或者Navigating Up with the App Icon需要考虑兼容性.下面介绍下split acti ...