[抄题]:

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

字符串中间还要加符号,头一次见

[奇葩corner case]:

两端都是空,只有root一个点也是能求和的,第一次见。下次二叉树求和的时候只要有点就能加

[思维问题]:

不知道怎么利用sum:变成参数即可

[一句话思路]:

还是没有汇总的traverse,把sum参与进来,把sum参数化变成一个参数就行了

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

path sum后续系列

[代码风格] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
//root is null
if (root == null) {
return false;
}
//only root is not null
if (root.left == null && root.right == null) {
return root.val == sum;
}
//remaning sum in left or sum in right
return (hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val));
}
}

112. Path Sum二叉树路径和的更多相关文章

  1. [LeetCode] 112. 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. [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)

    Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...

  3. LeetCode 112. Path Sum 二叉树的路径和 C++

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  4. LeetCode 112 Path Sum(路径和)(BT、DP)(*)

    翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...

  5. Leetcoede 112 Path Sum 二叉树

    二叉树的从叶子到根的和是否存在 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * ...

  6. LeetCode 112. Path Sum(路径和是否可为sum)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  7. [LeetCode] 112. Path Sum 路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  8. [LeetCode] 437. Path Sum III 路径和 III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

随机推荐

  1. bzoj 1185 [HNOI2007]最小矩形覆盖——旋转卡壳

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1185 矩形一定贴着凸包的一条边.不过只是感觉这样. 枚举一条边,对面的点就是正常的旋转卡壳. ...

  2. Eclipse 创建类时添加继承

    eclipse 中类的继承创建有两种方式: 1.手动敲代码通过 extends 关键字来继承 public class A extends B { } 2.在创建类的时候就选择好继承关系 点击 Fin ...

  3. FPGA的CNN加速,你怎么看?

    网上对于FPGACNN加速的研究已经很多了,神经网络的硬件加速似乎已经满大街都是了,这里我们暂且不讨论谁做的好谁做的不好,我们只是根据许许多多的经验来总结一下实现硬件加速,需要哪些知识,考虑哪些因素. ...

  4. POJ1745动态规划

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11622   Accepted: 4178 Des ...

  5. 【转】使用 JMeter 完成常用的压力测试

    本文介绍了 JMeter 相关的基本概念.并以 JMeter 为例,介绍了使用它来完成最常用的三种类型服务器,即 Web 服务器.数据库服务器和消息中间件,压力测试的方法.步骤以及注意事项.      ...

  6. angular的继承作用域通信

    本人学了一段时间的angular,angular之间怎样通信,我就总结以下几点,如果有哪位大神认为不对,敬请赐教. 1.父子之间的作用域进行通信 html <div ng-controller= ...

  7. 关于select的一个死循环

    #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <sys/sel ...

  8. C++ 新特性-右值引用

    作为最重要的一项语言特性,右值引用(rvalue references)被引入到 C++0x中.我们可以通过操作符“&&”来声明一个右值引用,原先在C++中使用“&”操作符声明 ...

  9. Oracle定时值执行存储过程

    declare      jobno number;    begin      dbms_job.submit(     jobno,     'p_dosomething;',  --what   ...

  10. 在单板上使用WIFI网卡的固件问题

    (在单板上使用WIFI网卡的固件问题)(我的wifi网卡是RT3070) (一般买的网卡说是支持LINUX免驱的话,那么在/lib/firmware/ 下一定有相应的固件) 我将USB网卡接入UBUN ...