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 ...
随机推荐
- Debian 7设备nginx周围环境、编译并安装和启动
/********************************************************************* * Author : Samson * Date ...
- Android 根据规划 Touch 分配和消费机制的事件
Android 中与 Touch 事件相关的方法包含:dispatchTouchEvent(MotionEvent ev).onInterceptTouchEvent(MotionEvent ev). ...
- [搜索] hdu 4016 Magic Bitwise And Operation
主题链接: http://acm.hdu.edu.cn/showproblem.php?pid=4016 Magic Bitwise And Operation Time Limit: 6000/30 ...
- Doug Lea
如果IT的历史,是以人为主体串接起来的话,那么肯定少不了Doug Lea.这个鼻梁挂着眼镜,留着德王威廉二世的胡子,脸上永远挂着谦逊腼腆笑容,服务于纽约州立大学Oswego分校计算机科学系的老大爷. ...
- S关于使用QL声明 找出同时满足多个tag拍摄条件设置算法
表结构 Tag Table:{tag_id, tag_name} #标签表 News Table:{news_id, title,......} #新闻列表 NewsTags Table:{tag ...
- CSS浏览器兼容性问题集()两
11.非常适合 高度适合于被改变时所述内目标高度的外层的高度不能自己主动调节,尤其是排队对象时margin 要么paddign 时. 例: #box {background-color:#e ...
- HDU 3117 Fibonacci Numbers(围绕四个租赁斐波那契,通过计++乘坐高速动力矩阵)
HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵高速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意: 求第n个斐波那契数的 ...
- poj 3744 Scout YYF I (可能性DP+矩阵高速功率)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5062 Accepted: 1370 Description YYF i ...
- java_eclipse_设置全局编码_utf-8_编译class指定_运行jar乱码解决_不依赖环境
简述: javac时指定 编码 UTF-8 [ javac -encoding UTF-8 Test.java],运行时 java 指定编码 UTF-8 这样就不会出现乱码问题[ javac ...
- 准备战争“软测试”之DB基础知识
"数据库"东西这个陌生和数据,进入提高班,从第二年开始接触,的项目还是自考的学习加起来也有3遍了.这仅仅是一个開始,软考又要对数据库进行全面的分析,那么如今就让我们再一次剖析它吧! ...