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: ------------------------------------------------ ...
- 2014年辛星Javascript解读第四节 流程控制语句
上一节我们介绍了函数,本小节我们介绍一下流程控制语句,对于不论什么一门编程语言来说,流程控制都是很重要的,也就是我们常说的顺序结构.选择结构和循环结构. ************选择结构******* ...
- ASP.NET Identity 身份验证和基于角色的授权
ASP.NET Identity 身份验证和基于角色的授权 阅读目录 探索身份验证与授权 使用ASP.NET Identity 身份验证 使用角色进行授权 初始化数据,Seeding 数据库 小结 在 ...
- lol盒子重点内容
//AFN函数 - imageview载入网络图片而且获取图片,获取之后存储到手机 [image setImageWithURLRequest:[NSURLRequest requestWithU ...
- jsp跳转后台代码页的简易方式~
jsp跳转到代码隐藏页.有几种方法,例如,: action方式: jquery方式,码如下面: function regCust(){ $('#containerFRM').form( ...
- C++11实现模板手柄:委托构造函数、defaultkeyword分析
C++11.使用委托构造函数.和高速变量初始化,defaultkeyword重新声明默认构造函数,回答pod状态. 分析与推荐的方法. 到目前为止,VS2012和2013异常声明兼容还是停留在通信代码 ...
- 【通过做专题研习Android】知识点:SharedPreferences
Ⅰ. 一个简短的引论 很多时候我们需要开发软件,为用户提供软件参数设置功能,比如,我们经常使用 QQ.用户可以设置自己是否同意加入一个陌生人为好友.对于软件的配置参数的存储,假设window採用ini ...
- VC中MessageBox的常见用法
一.关于MessageBox 消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合. 1.MessageBox("这是一个最简单的 ...
- JavaScript中的try...catch和异常处理
在JavaScript可以使用try...catch来进行异常处理.例如: try { foo.bar();} catch (e) { alert(e.name + ": " + ...
- How many prime numbers(素数)
Problem Description Give you a lot of positive integers, just to find out how many prime numbers t ...