LeetCode_Path Sum II
一.题目
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] ]
Discuss
二.解题技巧
这里面有一个技巧,在进入每个结点的时候。先将该结点的值push到vector中。在退出时间该结点的值pop出来,这样就能够避免有时会忘记pop结点的值的情况。
三.实现代码
#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 II相关推荐
- 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 ...
- Digit Sum II( ABC044ARC060)
问题 G: Digit Sum II 时间限制: 1 Sec 内存限制: 128 MB 提交: 36 解决: 11 [提交][状态][讨论版][命题人:admin] 题目描述 For intege ...
- [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 ...
- Lintcode: k Sum II
Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...
- 113. Path Sum II
/** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 1.长度标准:无 ...
- 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/ 题解 简单的遍历二叉树,不解 ...
- Leetcode: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- 每天一算:Two Sum II
leetcode上第75号问题:Two Sum II 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 ind ...
- 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 ...
最新文章
- 80热敏打印机打印TxPrnMod.dll
- C#中split的用法
- java学习(31):for循环
- 一文搞懂物联网Modbus通讯协议
- 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure(对于二分类问题)
- DPDK内存篇(一): 基本概念
- Python DearPyGui 多线程与异步
- ubuntu命令行打开vscode-insider_系统小技巧:迁移Wubi安装的ubuntu到Windows 10
- 职称计算机考试f11,2016全国职称计算机考试PowerPoint单选试题3
- Android 国内集成使用谷歌地图
- Python爬虫之链家二手房数据爬取
- Dissect Eclipse Plugin Framework
- 用matlab绘制三维图和三视图
- MeteoEarth全球天气
- 厦门大学计算机科学与工程学院,厦门大学计算机系
- 百度编辑器的样式手机端配置
- 模板匹配理论的优缺点,模板匹配和神经网络
- zip直链生成网站_防止赖床的闹钟软件、免费好用的看图软件、色卡生成器 今天有什么?...
- sim7600ce拨号上网
- 因果系列文章(2):因果推断初探
热门文章
- OpenCV使用cv :: CascadeClassifier类检测视频流中的对象的实例(附完整代码)
- 在D-Bus适配器中声明槽
- C++ Heavy Light Decomposition重轻分解的实现算法(附完整源码)
- QT的QGroupBox类的使用
- QT的QFutureIterator类的使用
- QT的QColorDialog类的使用
- C语言标准字符char和字符串string
- 标签的宽度_27 表格标签
- 「ProtocolBuffers2」ProtocolBuffers2 c++简易入门
- js获取url中的参数,url中传递中文的时候通过js解码的方式