leetcode112
/**
* 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
{
Stack<TreeNode> S = new Stack<TreeNode>();
List<List<TreeNode>> list = new List<List<TreeNode>>();
private void postNode(TreeNode node)
{
if (node != null)
{
S.Push(node); if (node.left != null)
{
postNode(node.left);
} if (node.right != null)
{
postNode(node.right);
} if (node.left == null && node.right == null)
{
list.Add(S.ToList());
}
S.Pop();
}
} public bool HasPathSum(TreeNode root, int sum)
{
postNode(root); foreach (var l in list)
{
var psum = ;
foreach (var d in l)
{
psum += d.val;
}
if (psum == sum)
{
return true;
}
} return false;
}
}
https://leetcode.com/problems/path-sum/#/description
补充一个python的实现:
class Solution:
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
if root == None:
return False
if root.left == None and root.right == None and root.val == sum:
return True
return self.hasPathSum(root.left,sum-root.val) or self.hasPathSum(root.right,sum-root.val)
leetcode112的更多相关文章
- [Swift]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 ...
- LeetCode112:Path Sum
正常写法 bool HasPathSum(TreeNode root, int sum) { bool ret=false; if(root==null)return false; if(root.l ...
- LeetCode112.路径总和
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22 ...
- 第34-1题:LeetCode112. Path Sum I
题目 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum ...
- 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 ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
- LeetCode通关:连刷三十九道二叉树,刷疯了!
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- springboot模糊查询
在学习MyBatis过程中想实现模糊查询,可惜失败了.后来上百度上查了一下,算是解决了.记录一下MyBatis实现模糊查询的几种方式. 数据库表名为test_student,初始化了几条记录,如图: ...
- hdu 6158 The Designer( 反演圆)
The Designer Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- Eclipse 项目有红惊叹号
Eclipse 项目有红感叹号原因:显示红色感叹号是因为jar包的路径不对 解决:在项目上右击Build Path -> Configure Build Paht...(或Propertise- ...
- HDU 3986
http://acm.hdu.edu.cn/showproblem.php?pid=3986 从开始的最短路里依次删一条边,求新的最短路,求最长的最短路 删边操作要标记节点以及节点对应的边 #incl ...
- python线程的GIL问题(全局解释器锁)
造成原因: python ---> 支持线程操作 --->IO的同步和互斥 --> 加锁 ----> 超级锁,给解释器加锁--->解释器同一时刻只能解释一个线程 造成的后 ...
- pidgin-lwqq
今天第一次用pidgin登上了qq,好神奇. 从网上看到的,说是pidgin发布了支持webqq协议的lwqq,按照解释之后安装了. 感觉好神奇啊. 一个绿色的小企鹅^^ 今天有看到了一个大牛,我觉得 ...
- BZOJ2938: [Poi2000]病毒(AC自动机)
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1678 Solved: 849[Submit][Status][D ...
- SEO -- WP如何建立SiteMap
站点地图对网站的seo优化有着相当重要的作用,而WordPress的优势就是插件特别的多,也特别符合蜘蛛的口味,在wp上建立站点地图是相当简单的事情,只需要一款插件和几步简单的配置 Google XM ...
- 【转】Linux 图形界面与命令行模式切换
原文网址:http://blog.csdn.net/ldl22847/article/details/7600368 Tip:使用环境VMware Workstation OS:CentOS 6 ...
- jeecms一些经典标签
http://www.121ask.com/thread-5512-1.html [@cms_channel_list]标签详细介绍 http://www.jeecmstheme.com/2014/0 ...