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 ...
随机推荐
- 关于TCP/IP协议栈(转)
1. TCP/IP协议栈 与OSI参考模型不同,TCP/IP协议栈共有4层,其中网络接口层对应OSI中的物理层和数据链路层,应用层对应OSI中的应用层.表示层和会话层. 在网络接口层的主要协议有:AR ...
- Session中StateServer的使用方法
最近项目中用到 Session的StateServer模式,我们知道sessionState有四种模式:off,inProc,StateServer,SqlServer. 而StateServer 是 ...
- Dojo Mobile制定学习用品
Dojo Mobile开展 App技术开发QQ群:347072638 技术咨询.APP定制开发联系邮箱:messageloop@qq.com 时代在演变.技术在革新.无论你接受不接受. 初识Dojo ...
- word插入图片显示不完整的解决的方法
有时在编写word文档,插入图片时,会出现图不完整的情况. 解决方法是:选中图片,段落格式设置为单位行距(不是22磅),图片格式为嵌入式.问题解决.
- 有一定基础的 C++ 学习者该怎样学习 Windows 编程?
人的心理有个奇异的特性:一项知识一旦学会之后,学习过程中面临的困惑和不解非常快就会忘得干干净净,似乎一切都是自然而然,本来就该这种.因此,关于「怎样入门」这类问题,找顶尖高手来回答,未必能比一个刚入门 ...
- Android开源--MenuDrawer
开放的源地址:https://github.com/SimonVT/android-menudrawer 简单介绍:menudrawer是跟sliderMenu差点儿相同的一种框架,常被应用做设置界面 ...
- SpringMVC1
itRed You are never too old to set another goal or to dream a new dream. SpringMVC一路总结(一) SpringMVC听 ...
- Sqlserver2012 评估期已过解决问题
Sqlserver2012评估期已过问题解决 一.背景: 因为之前安装sqlserver2012忘记输入序列号,如今出现评估期已过的问题,前几天忙活半天,才解决,发现网 上叙述都非常凌乱,并且仅仅有 ...
- php_linux_ubuntu_安装mysql_apache_php
用apt-get方法安装mysql5 + Apache2 + PHP5+Phpmyadmin [建议] http://www.sudu.cn/info/html/edu/20080102/283439 ...
- 【百度地图API】小学生找哥哥——小学生没钱打车,所以此为公交查询功能
原文:[百度地图API]小学生找哥哥--小学生没钱打车,所以此为公交查询功能 任务描述: 有位在魏公村附近上小学的小朋友,要去北京邮电大学找哥哥.他身上钱很少,只够坐公交的.所以,百度地图API快帮帮 ...