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 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 && root.val == sum)
return true;
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
Java [Leetcode 112]Path Sum的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- 移动端页面使用rem来做适配
文/九彩拼盘(简书作者)原文链接:http://www.jianshu.com/p/eb05c775d3c6著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. rem介绍 rem(font ...
- 树形dp求树的重心
Balancing Act http://poj.org/problem?id=1655 #include<cstdio> #include<cstring> #include ...
- 【Asp.net MVC ---杂七杂八】
@RenderSection 母模板:_mainLayout.cshtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...
- POJ 2771 Guardian of Decency(求最大点独立集)
该题反过来想:将所有可能发生恋爱关系的男女配对,那么可以带出去的人数应该等于这个二分图的最大独立集 先要做一下预处理,把不符合要求的双方先求出来, company[i][j]表示i.j四个标准都不符合 ...
- JsRender系列demo-对null 和boolen类型数据的探讨
废话不说了,直接上代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...
- APT攻击
http://netsecurity.51cto.com/art/201211/363040.htm
- WAF安恒
http://wenku.baidu.com/view/c242927f581b6bd97e19ea1a.html?from=search
- Android Activity生命周期 onSaveInstanceState和onRestoreInstanceState
当某个activity变得“容易”被系统销毁时,该activity的onSaveInstanceState就会被执行,除非该activity是被用户主动销毁的,例如当用户按BACK键的时候. 注意上面 ...
- [2-sat]HDOJ3622 Bomb Game
题意:给n对炸弹,每对炸弹选其中一个爆炸. 每个炸弹爆炸的半径相同 圆不能相交, 求最大半径 2-sat简介 二分半径, 枚举n*2个炸弹 若i炸弹与j炸弹的距离小于半径*2 则建边 比如 第一对炸 ...
- BZOJ 1982 Moving Pebbles
首先我们假设只有两堆, 容易发现当且仅当两堆相等时,先手必败 否则先手必胜 然后我们猜测一下原因: ->当两堆相等时,无论先手怎么做,后手总能使两堆相等,且必败态为0,0 推广一下: 当所有的石 ...