leetco Path Sum II
和上一题类似,这里是要记录每条路径并返回结果。
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]
- ]
- 我们用一个子函数来递归记录,知道叶子节点才判断是否有符合值,有的话就记录。需要注意的是递归右子树之前要把左子树的相应操作去除(见注释)。
- /**
- * Definition for binary tree
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- void pathSum(TreeNode *root, vector<vector<int> > &ans, vector<int> tmp, int subsum, int sum)
- {
- if (!root) return ;
- if (!root -> left && !root -> right && subsum + root -> val == sum)
- {
- tmp.push_back(root -> val);
- ans.push_back(tmp);
- }
- if (root -> left)
- {
- tmp.push_back(root -> val);
- subsum += root -> val;
- pathSum(root -> left, ans, tmp, subsum, sum);
- tmp.pop_back(); //因为判断右子树的时候不需要左子树的和
- subsum -= root -> val;
- }
- if (root -> right)
- {
- tmp.push_back(root -> val);
- subsum += root -> val;
- pathSum(root -> right, ans, tmp, subsum, sum);
- }
- }
- vector<vector<int> > pathSum(TreeNode *root, int sum)
- {
- vector<vector<int> > ans;
- vector<int> tmp;
- pathSum(root, ans, tmp, , sum);
- return ans;
- }
- };
其实效率好一些的是对tmp传入引用,例如vector<int> &tmp,那么此时每次记录结果或者左右递归之后都要有一个pop值,来保证tmp符合当前的要求:详见
- /**
- * Definition for binary tree
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- void pathSum(TreeNode *root, vector<vector<int> > &ans, vector<int> &tmp, int subsum, int sum)
- {
- if (!root) return ;
- if (!root -> left && !root -> right && subsum + root -> val == sum)
- {
- tmp.push_back(root -> val);
- ans.push_back(tmp);
- tmp.pop_back(); // 保持tmp
- }
- if (root -> left)
- {
- tmp.push_back(root -> val);
- subsum += root -> val;
- pathSum(root -> left, ans, tmp, subsum, sum);
- tmp.pop_back(); // 因为判断右子树的时候不需要左子树的和
- subsum -= root -> val; // 同上理
- }
- if (root -> right)
- {
- tmp.push_back(root -> val);
- subsum += root -> val;
- pathSum(root -> right, ans, tmp, subsum, sum);
- tmp.pop_back(); // 保持tmp
- }
- }
- vector<vector<int> > pathSum(TreeNode *root, int sum)
- {
- vector<vector<int> > ans;
- vector<int> tmp;
- pathSum(root, ans, tmp, , sum);
- return ans;
- }
- };
leetco Path Sum II的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【leetcode】Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 32. Path Sum && Path Sum II
Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II
Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...
- Path Sum,Path Sum II
Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...
- LeetCode之“树”:Path Sum && Path Sum II
Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...
随机推荐
- ASN.1 Encode an Object Identifier (OID) with OpenSSL
OID(Object Identifier) denotes an object. Examples: ------------------------------------------------ ...
- NSIS:迅雷5.8.6.600自由定制版脚本及下载
原文 NSIS:迅雷5.8.6.600自由定制版脚本及下载 虽然现在迅雷已升级到7.2版本,但作为一款下载工具,我们可能只是需要它的下载功能,所以,体积小资源占用也小的迅雷5便成了我们更好的选择.今天 ...
- svn常见错误汇总
comment中的换行.把换行去掉就可以了
- 【C/C++学院】(24)Oracle数据库编程--管理oracle
一.启动和停止oracle 停止和启动oracle须要切换到oracle用户才干够,其它用户都没有权限启动和停止oracle(包含root也没有权限). 1.执行sqlplus但不登录到oracle: ...
- hdu4288 Coder 2012成都网络赛 A题
题意:往集合里面添加删除数,集合中的数是按从小到大排列的,询问下标模5等于3的数的和. 记得当时这题不会做, 现在想简单多了,只要维护五个值和左右子树的size大小就行了. #define maxn ...
- ABP领域层——领域事件(Domain events)
ABP领域层——领域事件(Domain events) 基于DDD的现代ASP.NET开发框架--ABP系列之14.ABP领域层——领域事件(Domain events) ABP是“ASP.NET B ...
- hdu 1542 Atlantis 段树区,并寻求,,,尼玛真坑人数据,不要打开一小阵!
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- LeetCodeOJ. Maximum Depth of Binary Tree
见问题: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ 主题概述 Given a binary tree, find i ...
- 数学思想方法-sasMEMO(17)
SAS日期及时间格式 data _null_;input mydate YYMMDD10.;put mydate YYMMDDB10.;put mydate YYMMDDC10.;put mydat ...
- myql_链接丢失异常_mybaits _等框架_报错_The last packet successfully
mysql 8小时问题的解决方法 转发: 别看是英文 ,写的很好 ,才转 Use Hibernate + MYSQL database development, link timeout proble ...