LeetCode 113. Path Sum II 动态演示
给第一个目标值,返回一棵树从根到叶子所有和等于目标值的路径。
经典的深度优先算法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void helper(TreeNode* cur, int sum, int target, vector<int>& output, vector<vector<int>>& ret){
//a(cur)
//lk("root",cur)
//a(sum)
//a(target)
//dsp
if(!cur->left && !cur->right){
if(sum==target){
ret.push_back(output);
}
return;
} if(cur->left){
output.push_back(cur->left->val);
helper(cur->left, sum+cur->left->val, target, output, ret);
output.pop_back();
} if(cur->right){
output.push_back(cur->right->val);
helper(cur->right, sum+cur->right->val, target, output, ret);
output.pop_back();
}
} vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<int> output;
vector<vector<int>> ret;
//ahd(root)
//a(ret)
//a(output)
if(!root)
return ret; output.push_back(root->val);
helper(root, root->val, sum, output, ret);
return ret;
}
};
程序运行动态演示 http://simpledsp.com/FS/Html/lc113.html
LeetCode 113. Path Sum II 动态演示的更多相关文章
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 113. Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- leetcode 113. Path Sum II (路径和) 解题思路和方法
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [leetcode] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- LeetCode 113. Path Sum II路径总和 II (C++)
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- Leetcode 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- leetcode 113 Path Sum II ----- java
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [leetcode]113. Path Sum II路径和(返回路径)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- 模板 - 可持久化无旋Treap
空间消耗非常玄学,有多大开多大就完事了.其实是因为单次操作可能会有数次Merge和Split操作,按照下面的版本的话Merge和Split都进行复制,所以一次操作可能复制了4个版本. 四个函数式查询, ...
- Sql中使用Case when
SELECT CASE WHEN Column IS NOT NULL THEN '情况1' ELSE '情况2' END AS '列名' , FROM dbo.Table
- RAS
Reliability 高可靠性 Availability 高可用性 Serviceability 高服务性
- c++函数参数或返回值为函数指针
C++中函数指针的形式为:返回值类型 + 参数类型,函数没有值类型,但是却可以声明函数的指针,因为函数是可寻址的,存放在内存中的代码段,可以从指针访问. 函数指针可以声明为: void (*pF)(v ...
- amqp 抓包 不要在同一台机器
- 【CF】38E Let's Go Rolling! (dp)
前言 这题还是有点意思的. 题意: 给你 \(n\) (\(n<=3000\)) 个弹珠,它们位于数轴上.给你弹珠的坐标 \(x_i\) 在弹珠 \(i\) 上面花费 \(C_i\) 的钱 可以 ...
- LocalDateTime用法(jdk1.8 )
前言 最近看别人项目源码,发现Java8新的日期时间API很方便强大,所以转载该入门介绍博客,记录一下. 使用新时间日期API的必要性 在java8以前,或许: 当你在做有关时间日期的操作时,你会想到 ...
- Linux命令行工具之vmstat命令
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484608.html vmstat是一款指定采样周期和次数的功能性监测工具,可以使用它监控进程上下文 ...
- Git关联JIRA的issue
指导文章 http://www.51testing.com/html/30/n-3724930.html http://{$host_url}/help/user/project/integratio ...
- hdu 6044 : Limited Permutation (2017 多校第一场 1012) 【输入挂 组合数学】
题目链接 参考博客: http://blog.csdn.net/jinglinxiao/article/details/76165353 http://blog.csdn.net/qq_3175920 ...