[LeetCode116]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 {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public bool HasPathSum(TreeNode root, int sum) {
if(root == null)
return false;
if(root.left == null && root.right == null && root.val == sum)
return true;
else if(root.left == null && root.right == null && root.val != sum)
return false;
return HasPathSum(root.left, sum - root.val) || HasPathSum(root.right, sum - root.val);
}
}
[LeetCode116]Path Sum的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- [LeetCode] Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- Path Sum
需如下树节点求和 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 JavaScript实现 window ...
- [leetcode]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
随机推荐
- 利用Gearman实现并发查询(Multi-Query)
这个样例是想从数据库查询出几个结果集,一般的做法是,一个接一个的发送查询,然后汇总结果进行输出. 以下我们利用Gearman的gearman_client_run_tasks实现并发的查询,gearm ...
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...
- 抽出SqlHelper
什么叫SqlHelper,通过简单的翻译,能够获得这是对Sql的帮助,那么它究竟对我们的Sql做出了什么样的帮助呢? 在一款软件的设计编程中,总是会对数据库有连接的.假设你用的是SqlServer的数 ...
- 【转】真正的Acmer
上海交大 戴文渊 大牛写的东西,建议大家看看 yiyiyi4321 2007-07-10 13:49:30.0 http://dwyak.spaces.live.com/?_c11_BlogPart_ ...
- Lucene全文检索的【增、删、改、查】 实例
创建索引 Lucene在进行创建索引时,根据前面一篇博客,已经讲完了大体的流程,这里再简单说下: Directory directory = FSDirectory.open("/tmp/t ...
- BZOJ 1109 POI2007 堆积木Klo LIS
题目大意:给定一个序列,能够多次将某个位置的数删掉并将后面全部数向左串一位,要求操作后a[i]=i的数最多 首先我们如果最后a[i]=i的数的序列为S 那么S满足随着i递增,a[i]递增(相对位置不变 ...
- OCP读书笔记(19) - 数据库空间管理
传输表空间:将linux下的数据库中的test表空间传输到windows平台下的数据库 在传输表空间前,先确定一下源库与目标数据库字符集一致: select * from nls_database_p ...
- python : 批量下载R语言库包
soupR.py 代码例如以下 # -*- coding: cp936 -*- import urllib import urllib2 import os, re from BeautifulSou ...
- uvalive4015 (树上背包)
给一棵树,边上有权值,然后给一个权值x,问从根结点出发, 走不超过x的距离,最多能经过多少个结点. 走过的点可以重复走,所以可以从一个分支走下去,然后走回来,然后再走另一个分支 dp[u][j][0] ...
- HDU ACM 2845 Beans->动态规划
意甲冠军: 1. 对于每一行是,对不相邻的同一时间数取: 2.它是相同的列,相邻行不能同时服用: 3.因此,我们可以得到状态方程:dp[i]=dp[i-1]>(dp[i-2]+a[i])?dp[ ...