Path Sum II (Find Path in Tree) -- LeetCode
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]
]
class Solution {
public:
void help(vector<vector<int> >& res, TreeNode* root, vector<int>& path, int target)
{
TreeNode *left = root->left, *right = root->right;
path.push_back(root->val);
if (!left && !right && target == root->val)
res.push_back(path);
if (left)
help(res, left, path, target - root->val);
if (right)
help(res, right, path, target - root->val);
path.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int> > res;
if (!root) return res;
vector<int> path;
help(res, root, path, sum);
return res;
}
};
Path Sum II (Find Path in Tree) -- LeetCode的更多相关文章
- [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 ...
- [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 ...
- 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】113. 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 ...
- 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: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- 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 ...
随机推荐
- Linux忘记root密码的解决办法
这里以centos6为例: 第一步:先将系统重新启动,在读秒的时候按下任意键就会出现如下图的菜单界面: 第二步:按下『e』就能够进入grub的编辑模式,如图: 第三步:将光标移动到kernel那一行, ...
- Spider_Man_6 の Scrapy_Downloader Middleware(针对一下🐷🐷🐷)
下载器中间件(Downloader Middleware) 下载器中间件是介于Scrapy的request/response处理的钩子框架.是用于全局修改Scrapy request和response ...
- win10系统安装之GHOST还原(转+编辑)
注意*:在以下操作中,你可能需要分区你的原来系统盘,如果是重装的话.现在我们使用SSD固态做系统盘盘,这个分区的话,点选mbr重新引导,以及对齐复选框. 如果前面过程都没问题,在安装过程中出现 ...
- KMS激活windows
slmgr /skms 106.0.6.209 slmgr /ato .查看当前系统是否永久激活,命令的作用是查看当前许可证状态的截止日期 slmgr.vbs -xpr 查看Windows正式版产品密 ...
- C++中include<> 与 include" " 的区别
<>时先去系统目录中找头文件,如果没有再到当前目录下找.所以像标准的头文件 stdio.h, stdlib.h等都用<>; ""则首先到当前目录下找,如果找 ...
- busybox根文件系统使用记录
1.DHCP功能配置 1.1.配置Linux内核使能DHCP相关选项: [*]Networking support --> Networking support Networking optio ...
- h5 Visibility API总结
最近活动中的小游戏,有涉及页面隐藏或app后台运行时候,暂停游戏的功能,使用了h5的Visibility API,在此总结如下: 两个属性 document.hidden (Read only) 如果 ...
- 旅行规划(travel)
题目描述 OIVillage 是一个风景秀美的乡村,为了更好的利用当地的旅游资源,吸引游客,推动经济发展,xkszltl 决定修建了一条铁路将当地 nnn 个最著名的经典连接起来,让游客可以通过火车从 ...
- 如何使用 JSP JSTL 显示/制作树(tree) 菜单
JSTL里面并没有直接制作tree菜单的元素,因此递归是JSP JSTL显示/制作tree菜单的唯一方法. 以下详述如何制作tree菜单. 首先,在主页面里面增加包含制作树菜单的jsp,例如: 在my ...
- P1613 跑路 (最短路,倍增)
题目链接 Solution 发现 \(n\) 只有 \(50\), 可以用 \(floyd\) . 然后 \(w[i][j][l]\) 代表 \(i\) 到 \(j\) 是否存在 \(2^l\) 长的 ...