/**
* 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. mybatis之org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'time' in 'class java.lang.String'

    mybatis接口 List<String> getUsedCate(String time); 配置文件 <select id="getUsedCate" pa ...

  2. vim with space-vim

    space-vim https://github.com/liuchengxu/space-vim macOS # homebrew /usr/bin/ruby -e "$(curl -fs ...

  3. NYOJ 720 DP+二分

    项目安排 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个 ...

  4. UVA-1660 Cable TV Network (最小割)

    题目大意:给一张n个点.m条边的无向图,求最小点割集的基数. 题目分析:求无向图最小点割集的基数可以变成求最小割.考虑单源s单汇t的无向图,如果要求一个最小点集,使得去掉这个点集后图不再连通(连通分量 ...

  5. oracle 10g 用dbms_xmlgen将数据表转成xml格式

    oracle 10g 用dbms_xmlgen将数据表转成xml格式 oracle 10g 用dbms_xmlgen将数据表转成xml格式 oracle用plsql将sql查询的所有数据导出为xml

  6. POJ 1797 kruskal 算法

    题目链接:http://poj.org/problem?id=1797 开始题意理解错.不说题意了. 并不想做这个题,主要是想测试kruskal 模板和花式并查集的正确性. 已AC: /* 最小生成树 ...

  7. bzoj2120

    题解: 可修改莫队 我们加入一个时间T 然后在排序的时候考虑一下时间 在计算的时候也要考虑 代码: #include<bits/stdc++.h> using namespace std; ...

  8. ACCESS_TOKEN与FRESH_TOKEN

    OAuth1.0中的access_token过期时间通常很长,安全性差.于是OAuth2.0推出了refresh_token. OAuth2.0中,客户端用账户名,密码经过一定方式(比如先请求code ...

  9. ehci ohci 驱动逻辑

    1. EHCI ehci_platform_probe: platform_get_irq usb_hcd_request_irqs usb_hcd_irq 使用的是ehci的irq hcd-> ...

  10. windows7安装django并创建第一个应用

    1.安装django 1.1.下载Django包 https://www.djangoproject.com/download/https://www.djangoproject.com/m/rele ...