题目描述:

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.

解题思路:

递归法。

代码描述:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null)
return false;
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);
}
}

  

Java [Leetcode 112]Path Sum的更多相关文章

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

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

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

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

  6. leetcode 112 Path Sum ----- java

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

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

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

  9. [Leetcode]112. Path Sum -David_Lin

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

随机推荐

  1. 【BZOJ】【2132】圈地计划

    网络流/最小割 Orz Hzwer 这类大概是最小割建模中的经典应用吧…… 黑白染色,然后反转黑色的技巧感觉很巧妙!这个转化太神奇了…… /****************************** ...

  2. [CF]codeforces round 369(div2)

    *明早起来再贴代码 A [题意] 给定n*5的方格 将横向的相邻两个变成+输出 [题解] ... B [题意] 一个n*n的正整数矩阵,有且仅有一个数为0 ,在这个位置填上一个数,使得每一列的和 每一 ...

  3. hlsl 的tex函数

    texCUBE http://msdn.microsoft.com/en-us/library/windows/desktop/bb509687(v=vs.85).aspx

  4. Javascript 绝对定位和相对定位

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  5. WAF安恒

    http://wenku.baidu.com/view/c242927f581b6bd97e19ea1a.html?from=search

  6. zoj 3599 Game 博弈论

    K倍动态减法游戏!!! 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4683 代码如下: #include<ios ...

  7. ios 关于StoryBoard 的简易使用说明

    ios 关于StoryBoard 的简易使用说明 http://www.tuicool.com/articles/FNRruy

  8. 为Android Studio 项目手动下载gradle

    在http://developer.android.com/samples/index.html上下载的例子,导入Android Studio的时候,第一件事就是下载项目对应版本的gradle.gra ...

  9. 电容值E系列标称方法

    本节首先介绍常用的E系列标称方法,然后介绍电阻.电容器.电感器.二极管的分类.性能和识别方法,以及简单的实用电路. 一.E系列标称方法 厂家生产的电阻器,并不是包含任何阻值,就像人民币,只有1.2.5 ...

  10. python 利用smtp发送邮件,html格式

    def send_mail(to_list, sub, context):#sentmail to the maillist ''' to_list: 发送给谁 sub: 主题 context: 内容 ...