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)
return sum==root.val; return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val); }
}

leetcode 112 Path Sum ----- java的更多相关文章

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

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

    Hibernate 一个框架; 一个 Java 领域的持久化框架; 一个 ORM 框架 ORM(Object/Relation Mapping): 对象/关系映射 –ORM的思想:将关系数据库中表中的 ...

  2. 【转】CentOS yum安装和卸载软件的使用方法

    在CentOS yum安装和卸载软件的使用方法安装方法安装一个软件时.   CentOS yum -y install httpd安装多个相类似的软件时   CentOS yum -y install ...

  3. Windows 技术预览版 - 传言中的Win 10

    http://windows.microsoft.com/zh-cn/windows/preview-iso Windows Technical Preview 产品密钥: NKJFK-GPHP7-G ...

  4. KVO的内部实现原理

    kvo概述 kvo,全称Key-Value Observing,它提供了一种方法,当对象某个属性发生改变时,允许监听该属性值变化的对象可以接受到通知,然后通过kvo的方法响应一些操作. kvo实现原理 ...

  5. <转>thinkphp自动验证无效的问题

    新手入门thinkphp,试用自动验证表单输入数据功能,却发现怎么都不能调用自动验证,自动验证无效,原因竟是一个小细节的疏忽,学习一定要细心啊! Action方法: IndexAction下的adds ...

  6. 2013年7月份第3周51Aspx源码发布详情

    批量重命名文件工具源码  2013-7-19 [VS2010]功能介绍:这是一个新型的文件重命名,主要用了TreeView(树形视图)来选择文件夹,批量进行文件重命名.其中,有"编号在前,编 ...

  7. linux基础命令学习(六)DHCP服务器配置

    工作原理:        1.客户机寻找服务器:广播发送discover包,寻找dhcp服务器        2.服务器响应请求:单播发送offer包,对客户机做出响应.提供客户端网络相关的租约以供选 ...

  8. selectors实现高并发

    1. 下面的例子,客户端给服务端发送消息,服务端把消息返回 server #!/usr/bin/env python import selectors import socket import tim ...

  9. python3爬虫初探(五)之从爬取到保存

    想一想,还是写个完整的代码,总结一下前面学的吧. import requests import re # 获取网页源码 url = 'http://www.ivsky.com/tupian/xiaoh ...

  10. HDU-4255 BFS 最短路

    题意:蛇形填数,然后素数处是障碍,给你起点终点,求步数: 思路:其实就是bfs,关键是将数字转换成位置比较难: bfs其实比较简单,就是固定的思路,固定的步骤: 模板: ][] = {{-, }, { ...