/**
* 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的更多相关文章

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

  2. LeetCode112:Path Sum

    正常写法 bool HasPathSum(TreeNode root, int sum) { bool ret=false; if(root==null)return false; if(root.l ...

  3. LeetCode112.路径总和

    给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22 ...

  4. 第34-1题:LeetCode112. Path Sum I

    题目 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例:  给定如下二叉树,以及目标和 sum ...

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

  6. leetcode_二叉树篇_python

    主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...

  7. LeetCode通关:连刷三十九道二叉树,刷疯了!

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...

随机推荐

  1. windows电脑使用技巧及常用CMD

    管理用户和组 win+R,输入 compmgmt.msc 本地用户和组->用户 本地安全策略 win+R,输入 secpol.msc 本地策略->安全选项,启用禁止空密码登录如下图,可以防 ...

  2. 过滤器系列(一)—— Bloom filter

    因为要做过滤器相关内容,最近读了一些过滤器方面的文章,准备从中提取主要思想写几篇博客. 作为这系列的第一篇文章,首先得讲一下过滤器是干什么用的.从历史发展来看,过滤器最早出现是作为散列表的替代品,那么 ...

  3. run jdeveloper, unable to create an instance of the Java Virtual Machine Located at path:

    刚才打开 jdevW.exe 时提示如下错误: Unable to create an instance of the Java Virtual MachineLocated at path:x:\x ...

  4. vim中将tab 设置成4个空格

    在.vimrc中添加以下代码后,重启vim即可实现按TAB产生4个空格:set ts=4  (注:ts是tabstop的缩写,设TAB宽4个空格)set expandtab 对于已保存的文件,可以使用 ...

  5. 057——VUE中vue-router之路由参数默认值的设置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. PyalgoTrade 计算权重平滑平均价(三)

    本节介绍如何使用收盘价的SMA价格的策略 from pyalgotrade import strategy from pyalgotrade.barfeed import yahoofeed from ...

  7. BZOJ1835: [ZJOI2010]base 基站选址【线段树优化DP】

    Description 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄 ...

  8. spring-security-4 (1)介绍

    一.什么是spring security? spring security是基于spring开发的为JavaEE企业级应用提供安全服务的框架.安全服务主要是指 认证(Authentication)和  ...

  9. 6-19 Count Connected Components(20 分)

    Write a function to count the number of connected components in a given graph. Format of functions: ...

  10. 51nod 算法马拉松4 D装盒子(网络流 / 二分图最优匹配)

    装盒子   基准时间限制:1 秒 空间限制:131072 KB 分值: 160 有n个长方形盒子,第i个长度为Li,宽度为Wi,我们需要把他们套放.注意一个盒子只可以套入长和宽分别不小于它的盒子,并且 ...