/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
if (root == NULL)
return ;
return Sum(root, , sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
}
private:
//pre为前面节点的和,cur为前面加上现在遍历到的节点;
int Sum(TreeNode* root, int pre, int sum){
if (root == NULL)
return ;
int cur = pre + root->val; return (cur == sum) + Sum(root->left, cur, sum) + Sum(root->right, cur, sum);
}
};
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1 Return 3. The paths that sum to 8 are: 1. 5 -> 3
2. 5 -> 2 -> 1
3. -3 -> 11

You are given a binary tree in which each node contains an integer value.

Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

【easy】437. Path Sum III 二叉树任意起始区间和的更多相关文章

  1. 【easy-】437. Path Sum III 二叉树任意起始区间和

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  2. 47. leetcode 437. Path Sum III

    437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...

  3. 437. Path Sum III

    原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...

  4. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  5. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

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

  7. LeetCode 437. Path Sum III (路径之和之三)

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

  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 437 Path Sum III 路径和

      相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...

随机推荐

  1. windows一键安装包的升级禅道

    如果你现在使用的是windows xampp 集成运行环境,那么请按照下面的步骤进行: 一.升级步骤: 下载新的源代码包(zip格式).(注意,不是.exe的集成运行环境,如果你下载这个,会造成数据被 ...

  2. (三)jdk8学习心得之方法引用

    三.方法引用 https://www.jianshu.com/p/c9790ba76cee 这边博客写的很好,可以首先阅读,在这里感谢这篇文章的博主. 1. 格式 调用者::调用者具备的方法名 2. ...

  3. jQuery的deferred对象实战应用(附:Exchar动态多条数据展示并在topic展示详细数据)

    解决三个后台请求都成功后先比较数据再处理数据的需求 今天碰到了一个问题,我需要创建一个图表,但是需要请求三个接口才能比较出指标数据,于是就看到了deferred对象 理论的补充在这里:http://w ...

  4. Flutter内置ICON

    由于有时打不开flutter的icon官网 https://material.io/tools/icons/?style=baseline 截图存下icon 如果看不清  Ctrl +   恢复Ctr ...

  5. Centos7 安装gitLab

    我这里使用的是centos 7 64bit,我试过centos 6也是可以的! 1. 安装依赖软件 yum -y install policycoreutils openssh-server open ...

  6. MySQL架构备份之双机热备

    M--S架构:实现双机热备(AB复制) 1.可以降低master读压力 2.可以对数据库做“热备”,热备只能解决硬件master硬件故障,软件故障等重大故障问题,但无法解决人为误操作导致的逻辑故障(列 ...

  7. Java Socket、计算机网络

    一个服务器对应一个客户端 http://blog.51cto.com/wangdy/1588379 https://www.cnblogs.com/rocomp/p/4790340.html pack ...

  8. 当前标识没有对“C:\WINDOWS\Microsoft.NET\...”的写访问权限的解决办法

    1.需要重新注册IIS服务扩展,在开始运行中输入以下命令运行:aspnet_regiis -i 32位的Windows: --------------------------------------- ...

  9. Springboot自定义异常处理

    1.自定义异常类 import lombok.Data; @Data public class UserException extends RuntimeException { private Lon ...

  10. “不能在dropdownlist中选择多个项

    DropDownList.ClearSelection(); DropDownList.SelectedItem.Text = "value值";