给定一个二叉树,二叉树的每个节点含有一个整数。
找出路径和等于给定数的路径总数。
路径不需要从根节点开始,也不需要在叶节点结束,当路径方向必须是向下的(只从父节点到子节点)。
二叉树不超过1000个节点,节点的整数值的范围是[-1000000,1000000]。
示例:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
      10
     /  \
    5   -3
   / \    \
  3   2   11
 / \   \
3  -2   1
返回 3. 和等于8的路径有:
1.  5 -> 3
2.  5 -> 2 -> 1
3. -3 -> 11
详见:https://leetcode.com/problems/path-sum-iii/description/
C++:

方法一:

/**
* 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==nullptr)
{
return 0;
}
return helper(root,sum)+pathSum(root->left,sum)+pathSum(root->right,sum);
}
int helper(TreeNode *root,int sum)
{
int res=0;
if(root==nullptr)
{
return res;
}
if(sum==root->val)
{
++res;
}
res+=helper(root->left,sum-root->val);
res+=helper(root->right,sum-root->val);
return res;
}
};

方法二:

/**
* 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) {
int res=0;
vector<TreeNode*> out;
helper(root,sum,0,out,res);
return res;
}
void helper(TreeNode* node,int sum,int curSum,vector<TreeNode*> &out,int &res)
{
if(!node)
{
return;
}
out.push_back(node);
curSum+=node->val;
if(curSum==sum)
{
++res;
}
int t=curSum;
for(int i=0;i<out.size()-1;++i)
{
t-=out[i]->val;
if(t==sum)
{
++res;
}
}
helper(node->left,sum,curSum,out,res);
helper(node->right,sum,curSum,out,res);
out.pop_back();
}
};

参考:https://www.cnblogs.com/grandyang/p/6007336.html

437 Path Sum III 路径总和 III的更多相关文章

  1. 113 Path Sum II 路径总和 II

    给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径.例如,给定下面的二叉树和 sum = 22,              5             / \            4 ...

  2. LeetCode 113. Path Sum II路径总和 II (C++)

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  3. Leetcode113. Path Sum II路径总和2

    给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 ...

  4. 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...

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

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

  7. 437. Path Sum III

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

  8. 【LeetCode】437. 路径总和 III

    437. 路径总和 III 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点 ...

  9. Java实现 LeetCode 437 路径总和 III(三)

    437. 路径总和 III 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点 ...

随机推荐

  1. 理解Android ANR的触发原理(转)

    一.概述 ANR(Application Not responding),是指应用程序未响应,Android系统对于一些事件需要在一定的时间范围内完成,如果超过预定时间能未能得到有效响应或者响应时间过 ...

  2. 【转】LoadRunner监控 -- Linux的17个指标

    这17个指标根据需要设置,指标设置的越多,对服务器真实值影响越大,所以要秉承按需而设的原则.   1.Average load:Average number of processes simultan ...

  3. iPhone开发关于UDID和UUID的一些理解【转】

    原文地址:http://blog.csdn.net/xunyn/article/details/13629071 一.UDID(Unique Device Identifier) UDID是Uniqu ...

  4. Cg入门23: Fragment shader – UV动画(序列帧)

    让动画从1-9循环播放此纹理 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkF ...

  5. commons-fileupload、smartUpload和commons-net-ftp

    1.本地上传 在许多Web站点应用中都需要为用户提供通过浏览器上传文档资料的功能,例如,上传个人相片.共享资料等.在DRP中,就有这个一个功能,需要将对应的物料图片上传并显示.对于上传功能,其实在浏览 ...

  6. mac下Android Studio干净卸载

    1.卸载Android Studio,在终端(terminal)执行以下命令: rm -Rf /Applications/Android\ Studio.app rm -Rf ~/Library/Pr ...

  7. POJ3164 Command Network —— 最小树形图

    题目链接:https://vjudge.net/problem/POJ-3164 Command Network Time Limit: 1000MS   Memory Limit: 131072K ...

  8. Powershell 常见问题

    unapproved verbs WARNING: The names of some imported commands from the module 'todo' include unappro ...

  9. 如何完成DEDE CMS外部数据库调用|不同数据库调用数据

    dedecms如何完成2个数据库内容彼此调用?这是笔者今日要和我们共享的内容.百度了一大堆,大多语焉不详.常识有限,所以就说下笔者的做法, 能够还有其他有用的办法,欢送共享.笔者站点是dedecms5 ...

  10. Evernote相关技术介绍——mysql+lucene+tomcat

    Evernote服务 我们的服务由以下几个组件组成. 分片(NoteStore)   分片是Evernote服务的核心单元,用于存储用户的笔记.每个分片最多可以支撑30万个Evernote用户,并包含 ...