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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- 各种排序算法思想复杂度及其java程序实现
一.冒泡排序(BubbleSort)1. 基本思想: 设排序表长为n,从后向前或者从前向后两两比较相邻元素的值,如果两者的相对次序不对(A[i-1] > A[i]),则交换它们, 其结果是将最小 ...
- spring的懒加载和depends-on
①延迟初始化Bean(惰性初始化Bean)是指不提前初始化Bean,而是只有在真正使用时才创建及初始化Bean. 配置方式很简单只需在<bean>标签上指定 “lazy-init” 属性 ...
- 让FireFox支持window.event属性
场景描述: 在用户行为采集的过程中,需要侦听window下的event对象,根据事件类型做相应的过滤处理,但在firefox下window.event是未定义的: 问题分析: 要想获取event属性共 ...
- BZOJ3239 Discrete Logging
一道裸的BSGS题目(叫baby step, giant step) 从爱酱的blog里学来的,是一个很神的根号算法. 如果我们有hash的工具的话,就是O(sqrt(p))的,这里又用了一个map所 ...
- 转载 ORACLE中实现表变量的方法
源文地址:http://blog.itpub.net/750077/viewspace-2134222/ 经常看到SQLSERVER 中用表变量类型的方式就能做到缓存一个比较大的中间结果, 然后再对这 ...
- Bitdefender Total Security 2014 Free 6 Months & 12 month License Key
German Only – Bitdefender Total Security 2014 Free 6 Months Serial License Keyhttp://www.bitdefender ...
- WebGL编程指南案例解析之绘制三个点
//案例2.绘制3个点,将顶点数据存到缓冲区对象(gl.ARRAY_BUFFER)中,然后顶点着色器从里面读数据(3个顶点) //着色器将对这些顶点进行逐个解析, //第一个顶点给到顶点着色器,赋值给 ...
- 专业软件 —— Adobe Audition
0. 中英文对照 mute:静音,solo:独奏,arm to record:准备录音: reverb:混响: 1. 简介 Adobe Audition CS6原身为经典的音频后期处理软件Cool E ...
- c++ int 转 string 实现前缀补0
最近需要读取一些格式化路径的文件.路径文件名字最后是数字从0开始到100结束,但是占了4位,即0000到0100这样. 需要将int转成string的同时加上前缀0. 方法一: 用sprintf()函 ...
- jquery插件与扩展一
要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...