题目:

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 {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public bool HasPathSum(TreeNode root, int sum) {
if(root == null)
return false;
if(root.left == null && root.right == null && root.val == sum)
return true;
else if(root.left == null && root.right == null && root.val != sum)
return false;
return HasPathSum(root.left, sum - root.val) || HasPathSum(root.right, sum - root.val);
}
}

[LeetCode116]Path Sum的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  3. [LeetCode] Path Sum III 二叉树的路径和之三

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

  4. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  5. [LeetCode] Path Sum II 二叉树路径之和之二

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

  6. [LeetCode] Path Sum 二叉树的路径和

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

  7. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  8. Path Sum

    需如下树节点求和 5  /  \ 4     8  /     /  \ 11  13    4 / \     /  \  7    2      5   1 JavaScript实现 window ...

  9. [leetcode]Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

随机推荐

  1. GB2312引进和使用的字体

    一个:先上图看到的结果,下面的屏幕截图android在测试的结果"SD卡测试".."GPS测试"和其他字符24x24字体进来. 二:  1)简单介绍       ...

  2. python语言学习8——字符串和编码

    Unicode编码 计算机只能处理数字,如果要处理文本,就必须把文本转化为数字才能处理 有许多编码标准,但是不同的编码标准有时候会混乱,所以Unicode应运而生 Unicode把所有语言统一到一套编 ...

  3. python(abi) RPM DEB Download

    python(abi) RPM DEB Download python(abi) RPM DEB Download

  4. Android wear 初体验

    近期一直在研究android wear SDK,整体感受来说就是和现有的android 其它的开发SDK还是有非常多新的东西.比如手机终端与手表端的通信机制,手表端的UI规范.可是从开发本身来讲,还是 ...

  5. ElasticSearch+Kibana 索引操作

    ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...

  6. poj1155(树形dp)

    题目链接:http://poj.org/problem?id=1155 题意:电视台要直播一场比赛,电视网络刚好形成了一棵树,其中有M个为客户端,其他的为中转站,其中中转站与中转站以及中转站与客户端之 ...

  7. WebApi异常

    WebApi异常处理解决方案   前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定 ...

  8. 用XAML做网页!!—框架

    原文:用XAML做网页!!-框架 上一篇中我进行了一下效果展示和概述,此篇开始将重现我此次尝试的步骤,我想大家通过阅读这些步骤,可以了解到XAML网页排版的方法. 下面就开始编写XAML,首先来定义一 ...

  9. Linux应用环境实战05:在Ubuntu 14.10中借用Windows的字体 (转)

    阅读目录 设置系统字体 安装微软的英文字体 查看系统的配置文件 借用Windows的字体 编写配置文件 在前一篇随笔中,我详细讨论了字体的分类及用途,也以Fedora 20为例,展示了字体配置的思路和 ...

  10. 一般报java.lang.NullPointerException的原因有以下几种

    一般报java.lang.NullPointerException的原因有以下几种: ·字符串变量未初始化: ·接口类型的对象没有用具体的类初始化,比如: List lt; 会报错 List lt = ...