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 ...
随机推荐
- Jforum环境搭建
前提:搭建好JDK.JRE.Tomcat.数据库 1.之前安装了Navicat Premium,所以直接用这个创建名为jforum的MySQL数据库,默认密码为空,记得设置密码,因为Jforum要用到 ...
- winform小知识
1.得到控制台或者winform程序在debug目录下生成的exe路径 建议使用:AppDomain.CurrentDomain.BaseDirectory或者Assembly.GetExecutin ...
- (笔记) RealTimeRender[实时渲染] C2
@author: 白袍小道 @来源:RealTime Render @建议书籍:龙书.RealTimeR第四版.GPUGem和PRO (来源:暗影不解释) 引点 这一章关注的管线中的管道功能,而非实现 ...
- Error “can't use subversion command line client : svn” Probably the path to Subversion executable is wrong
错误提示如图. 大概意思就是SVN路径不对 解决方法如下: 首先下载Subversion 1.8.13(1.8) 下载链接(https://www.visualsvn.com/downloads/) ...
- django orderby
https://www.douban.com/group/topic/44329052/
- solr集群搭建(复制)
Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南) 1 什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引 ...
- 抓取HTML网页数据
(转)htmlparse filter使用 该类并不是一个通用的工具类,需要按自己的要求实现,这里只记录了Htmlparse.jar包的一些用法.仅此而已! 详细看这里:http://gundumw1 ...
- update-database -script
update-database -script 更新脚本生成失败? 项目选择的不对 update后面-database空格-script
- mplab xIde 编译成功,但不能生成Hex文件
设置不对 如果还不行,在设置下面
- 堆栈(Stacks)
堆栈(Stacks) 准备工作 安装Docker 1.13及以上版本 安装Docker Compose正如第三部分的准备工作. 安装Docker Machine正如第四部分的准备工作. 阅读第一部分的 ...