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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- 将input type="file" 类型的图片文件转成base64
带有图片的form表单上传数据是很麻烦的,因为图片通常都是和文字分开上传,这是很麻烦的,所有吧图片转成base64就可以和当成文字上传了.话不多少,看代码: 首先定义一个类型为file的input标签 ...
- localStorage(本地存储)使用总结
1.https://www.cnblogs.com/st-leslie/p/5617130.html (localStorage使用总结)
- 适配器 STL
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- android adapter的性能小结
一般adapter的做法会重写getView方法 比如 @Override public View getView(int position, View convertView, ViewGroup ...
- PHP网站自动化配置的实现
一直都在用yii2做项目,有在用自动化配置,但是没有自己去配置过.中午没事去看了下yii的初始化代码,发现都是php而已! yii2初始化项目代码 所以,我们做项目肯定是可以用php做的,于是我新建了 ...
- 配置Yaf
pecl里面的yaf最新测试版http://pecl.php.net/package/Yaf 安装pcre 要先安装pcre, Debian ubuntu执行 sudo apt-get install ...
- python-websocket-server hacking
/************************************************************************* * python-websocket-server ...
- 从U-Boot显示Logo到Android
/******************************************************************************* * 从U-Boot显示Logo到And ...
- C语言--第四次作业
作业要求一 (70分) 实践最简答的项目wordcount,必须完成其中的基本功能,若可以完成其他功能给予加分.完成后请将你的设计思路.主要代码写在本次作业博客里. 真的迷茫<(_ _)> ...
- 《DSP using MATLAB》 示例 Example 9.12
代码: %% ------------------------------------------------------------------------ %% Output Info about ...