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 思路:递归所有路径,如果到达叶子节点,并且此时sum=0,则返回true;
 /**
* 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 flase; //一开始如果传进来空引用,则返回false;
sum-=root.val;
if (root.left==null&&&root.right==null){ //如果已经到了叶子
if (sum==0) //如果此时sum=0,则返回true
return true; //否则返回false
else
return false;
}
//同时递归左右子树
return hasPathSum(root.left,sum)||hasPathSum(root.right,sum);
}
}

[Leetcode]112. Path Sum -David_Lin的更多相关文章

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

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

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

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

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

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

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

  8. Java [Leetcode 112]Path Sum

    题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

  9. (二叉树 DFS 递归) 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 ...

随机推荐

  1. WinCC OA基本概念

    WinCC OA 是一个模块化软件架构的系统.所需的功能由不同任务创建的特定单元处理.在WinCC OA中,这些单元称为管理器 - 管理器是软件自身的一些独立的处理过程. 图:WinCC OA系统由功 ...

  2. SQL功能分类

    DDL  数据定义语言:创建表 ,库,列 DML 数据操作语言:用来操作数据库中的记录 DQL 数据查询语言 :用来查询数据 DCL 数据控制语言:定义访问权限和安全级别 —————————————— ...

  3. 创建一个git仓库

    1.git init 使用git init命令初始化一个git仓库,git仓库会生成一个.git目录 git init 1.使用指定的目录作为我们的git仓库 git init newrepo 2.初 ...

  4. 图解Raft之日志复制

    日志复制可以说是Raft集群的核心之一,保证了Raft数据的一致性,下面通过几张图片介绍Raft集群中日志复制的逻辑与流程: 在一个Raft集群中只有Leader节点能够接受客户端的请求,由Leade ...

  5. 关于XML

    一.XML定义 XML(eXtensible Markup Language)即可扩展标记语言,它与HTML一样,都是处于SGML,标准通用语言.Xml是Internet环境中跨平台的,依赖于内容的技 ...

  6. [LeetCode] Length of Longest Fibonacci Subsequence 最长的斐波那契序列长度

    A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...

  7. 使用datagrip链接mysql数据库的报错问题.

    1. datagrip刚打开时候,选择风格是白是黑后, 会有一个选择什么数据库,有oracle...一大堆,别选错了.我的是mysql,不要选成了windows sql 和sql. 2 基本设置写完, ...

  8. Validator验证框架

    Validator验证框架 系统分析 在设计Validator验证框架时,需要明确以下问题. (1)当用户没有输入数据时,弹出英文提示信息. (2)当用户输入的数据长度大于系统设置的数据长度,弹出英文 ...

  9. jenkins:一键回滚站点集群

    最近在学习jenkins过程中整理了大量资料,都收录在<jenkins自动化工具使用教程>,但依然缺少一些具体实现细节. 这篇文章,介绍jenkins做集群回滚时的两个设计方案,让一键回滚 ...

  10. 在 ubuntu 中愉快的安装 Jenkins

    这篇文章详细的记录了在 ubuntu 中安装 Jenkins 的一步又一步,因为找了很多 Linux 下安装 Jenkins 的教程,不是很满意 所以决定自己写一篇以备后用(终于让我找到了Java 不 ...