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 ...
随机推荐
- [TabControl] TabControl控件的最佳实践,可以把一个窗体和用户控件添加进来
看下效果吧<ignore_js_op> 下面是一个公共的添加方法看代码 [C#] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 1 ...
- Mysql日期和字符的相互转换
今天从网上查到了一些关于MySQL数据库的日期转换函数的转换的用法,在这里记录一下: mysql日期和字符相互转换 date_format(date,'%Y-%m-%d') ------------- ...
- JavaScript基础1——在末尾添加节点
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JFreeChart与struts2整合实例
1. 3个jar包 jcommon,jfreechart,strust2-jfreechart-plugin 2 <?xml version="1.0" encoding=& ...
- find 查找目录下的文件
1. 命令功能 find命令用于查到目录下的文件,同时也可以调用其它命令执行相应操作. 2. 语法格式 find [-H] [-L] [-P] [-Olevel] [-D help|tree|sear ...
- Sass @warn
@warn 和 @debug 功能类似,用来帮助我们更好的调试 Sass.如: @mixin adjust-location($x, $y) { @if unitless($x) { @warn &q ...
- 转义字符\e
Windows 平台下,conio.h 中有许多操作控制台颜色.格式的函数.但是再 Linux 平台下却没有类似的函数.经过在网上的一番搜索,找到了解决此问题的方法——转义字符\e.注意,\e这种写法 ...
- centeros 安装maven 私服
1:下载nexus: 下载maven: 2:解压缩 配置maven环境变量 cd /etc/profile MAVEN_HOME=/usr/mavenexport MAVEN_HOMEexport P ...
- alert(1) to win 2
function escape(s) { s = s.replace(/"/g, '\\"'); return '<script>console.log("' ...
- 理解Promise (3)
在promise 的then 中我们不仅有 成功状态 失败状态,可能还有等待状态,所以我们要对等待状态进行处理 function Promise(executor) { let self = th ...