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.

Note: A leaf is a node with no children.

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.

题意:

给定二叉树和一个值,判断从根到叶是否存在一条路径,其路径和等于该值。

思路:

dfs

代码:

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

[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 二叉树的路径和

    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路径总和 (C++)

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

  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 二叉树的路径和 C++

    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(路径和)(BT、DP)(*)

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

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

随机推荐

  1. 用两条命令看出你买的H3C光模块是否是正品

    display transceiver manuinfo interfacedisplay transceiver interface从下文可以看出 1/0/26 1/0/27 2/0/26三个端口的 ...

  2. Java 迭代器 工具类

    迭代器:Iterator接口 //获取集合中的对象Iterator<E> iterator() interface Iterator { boolean hasNext(); Object ...

  3. python 9*9示例

    # 9*9 乘法表# def nine_nine():#     for i in range(1, 10):#         for j in range(1, i+1):#            ...

  4. AVL树Python实现

    # coding=utf-8 # AVL树Python实现 def get_height(node): return node.height if node else -1 def tree_mini ...

  5. JavaScript中的闭包与匿名函数

    知识内容: 1.预备知识 - 函数表达式 2.匿名函数 3.闭包 一.函数表达式 1.定义函数的两种方式 函数声明: 1 function func(arg0, arg1, arg2){ 2 // 函 ...

  6. c++官方文档-指针

    #include<stdio.h> #include<iostream> #include<queue> #include<map> #include& ...

  7. zabbixzabbix

    一,安装文档    https://www.zabbix.com/documentation/4.0/zh/manual/installation/requirements zabbix3.2.6安装 ...

  8. JAVA Spring 简单的配置和操作 ( 创建实体类, 配置XML文件, 调试 )

    < 1 > 实体类 Person package java_spring.modle; /** * 一个实体类( Person ) */ public class Person { pri ...

  9. 在spring引入log4j(非web项目)

    https://blog.csdn.net/u012578322/article/details/78012183 在spring中使用log4j 引入log4j软件包 配置log4j属性 加载log ...

  10. EF时间模糊查询

    public List<Vote> SelectVoteByTime(string time) { return db.Vote.ToList().Where(x => x.V_Be ...