一.题目

Path Sum II

Total Accepted: 46778 Total Submissions: 175830My Submissions

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

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

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

return

[[5,4,11,2],[5,8,4,5]
]
Show Tags

Have you met this question in a real interview?

Yes
No

Discuss
















二.解题技巧

    这道题和LeetCode_Path Sum类似,仅仅只是这道题须要找到全部和等于给定值的路径,因此不能在中间部分进行剪枝。必须遍历全然部的路径。

这里面有一个技巧,在进入每个结点的时候。先将该结点的值push到vector中。在退出时间该结点的值pop出来,这样就能够避免有时会忘记pop结点的值的情况。

    这样的做法的时间复杂度为O(n),空间复杂度为O(logn)。

三.实现代码

#include <iostream>
#include <vector>using std::vector;/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/struct TreeNode
{int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};class Solution
{
private:void pathSum(TreeNode* root, int sum, vector<vector<int> > &Result,vector<int> &TmpResult){if (!root){return;}if (!root->left && !root->right && root->val == sum){TmpResult.push_back(sum);Result.push_back(TmpResult);// pop the leaf nodeTmpResult.pop_back();return;}int SumChild = sum - root->val;TmpResult.push_back(root->val);pathSum(root->left, SumChild, Result, TmpResult);pathSum(root->right, SumChild, Result, TmpResult);// pop the current nodeTmpResult.pop_back();}public:vector<vector<int>> pathSum(TreeNode* root, int sum){vector<vector<int> > Result;vector<int> TmpResult;pathSum(root, sum, Result, TmpResult);return Result;}
};

四.体会

    这道题和LeetCode_Path Sum类似,解法也是同样的吗。仅仅是不能进行剪枝而已。
版权全部,欢迎转载。转载请注明出处,谢谢

LeetCode_Path Sum II相关推荐

  1. LeetCode 167. Two Sum II - Input array is sorted--Python解法

    题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...

  2. Digit Sum II( ABC044ARC060)

    问题 G: Digit Sum II 时间限制: 1 Sec  内存限制: 128 MB 提交: 36  解决: 11 [提交][状态][讨论版][命题人:admin] 题目描述 For intege ...

  3. [LeetCode]113.Path Sum II

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

  4. Lintcode: k Sum II

    Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...

  5. 113. Path Sum II

    /** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 1.长度标准:无 ...

  6. leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)

    题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...

  7. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  8. 每天一算:Two Sum II

    leetcode上第75号问题:Two Sum II 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 ind ...

  9. LeetCode 113. Path Sum II

    113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum eq ...

最新文章

  1. 80热敏打印机打印TxPrnMod.dll
  2. C#中split的用法
  3. java学习(31):for循环
  4. 一文搞懂物联网Modbus通讯协议
  5. 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure(对于二分类问题)
  6. DPDK内存篇(一): 基本概念
  7. Python DearPyGui 多线程与异步
  8. ubuntu命令行打开vscode-insider_系统小技巧:迁移Wubi安装的ubuntu到Windows 10
  9. 职称计算机考试f11,2016全国职称计算机考试PowerPoint单选试题3
  10. Android 国内集成使用谷歌地图
  11. Python爬虫之链家二手房数据爬取
  12. Dissect Eclipse Plugin Framework
  13. 用matlab绘制三维图和三视图
  14. MeteoEarth全球天气
  15. 厦门大学计算机科学与工程学院,厦门大学计算机系
  16. 百度编辑器的样式手机端配置
  17. 模板匹配理论的优缺点,模板匹配和神经网络
  18. zip直链生成网站_防止赖床的闹钟软件、免费好用的看图软件、色卡生成器 今天有什么?...
  19. sim7600ce拨号上网
  20. 因果系列文章(2):因果推断初探

热门文章

  1. OpenCV使用cv :: CascadeClassifier类检测视频流中的对象的实例(附完整代码)
  2. 在D-Bus适配器中声明槽
  3. C++ Heavy Light Decomposition重轻分解的实现算法(附完整源码)
  4. QT的QGroupBox类的使用
  5. QT的QFutureIterator类的使用
  6. QT的QColorDialog类的使用
  7. C语言标准字符char和字符串string
  8. 标签的宽度_27 表格标签
  9. 「ProtocolBuffers2」ProtocolBuffers2 c++简易入门
  10. js获取url中的参数,url中传递中文的时候通过js解码的方式