LeetCode 112. Path Sum(java)

原创 2018年03月07日 09:41:29

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.

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

三层开发基本概念介绍

 三层客户/服务器一:优点:灵活性和可扩展性。应用三层开发技术(分布式)的原因(参考书目: Delphi 5.x分布式多层应用系统篇)一:解决客户机/服务器结构的维护成本问题,改善客户机/服务器结构延...
  • coolriver
  • coolriver
  • 2002-11-17 11:51:00
  • 1209

【LeetCode-面试算法经典-Java实现】【112-Path Sum(路径和)】

【112-Path Sum(路径和)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree and a sum, determine if...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-11 07:40:22
  • 2990

LeetCode 112 — Path Sum(C++ Java Python)

题目: 题目翻译: 分析: C++实现: Java实现: Python实现: 感谢阅读,欢迎评论!...
  • dragon_dream
  • dragon_dream
  • 2014-04-03 16:01:19
  • 2430

LeetCode112 PathSum Java题解

题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi...
  • u012249528
  • u012249528
  • 2015-07-06 10:58:52
  • 580

LeetCode112——Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all...
  • booirror
  • booirror
  • 2015-01-13 18:34:45
  • 3398

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 a...
  • fly_yr
  • fly_yr
  • 2015-10-19 12:53:56
  • 564

Leetcode 437. Path Sum III 路径和3 解题报告

1 解题思想这道题就是给了一个二叉树和一个目标和sum 找出所有路径,这个路径的和等于sum,只允许从父节点到子节点的路线所以方法么,也就是最基本的dfs,不多说,对了可以看看之前相关的题目: L...
  • MebiuW
  • MebiuW
  • 2016-10-23 14:29:28
  • 7577

【leetcode c++】 112 Path Sum

Path Sum   Given a binary tree and a sum, determine ifthe tree has a root-to-leaf path such that a...
  • hqq39
  • hqq39
  • 2015-07-31 23:27:31
  • 436

[LeetCode]112. Path Sum

https://leetcode.com/problems/path-sum/ 求是否存在根节点到叶子节点的和为sum 如果当前位置为空就返回false,如果当前为叶子节点且值为sum,返...
  • gqk289
  • gqk289
  • 2017-02-04 18:36:15
  • 156

LeetCode Oj 112. Path Sum 解题报告

112. Path Sum My Submissions Question Total Accepted: 91133 Total Submissions: 295432 Dif...
  • bruce128
  • bruce128
  • 2016-02-22 13:58:45
  • 1317
收藏助手
不良信息举报
您举报文章:LeetCode 112. Path Sum(java)
举报原因:
原因补充:

(最多只允许输入30个字)