给第一个目标值,返回一棵树从根到叶子所有和等于目标值的路径。

经典的深度优先算法

/**
* 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 动态演示的更多相关文章

  1. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. [leetcode] 113. Path Sum II (Medium)

    原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. [集合]java中的 可变参数

    可变的参数类型,也称为不定参数类型.英文缩写是varargus,还原一下就是variable argument type.通过它的名字可以很直接地看出来,这个方法在接收参数的时候,个数是不定的. pu ...

  2. Photoshop制作Android UI:怎样从大图片中准确剪切出圆角正方形 图片

    如题所看到的,最初我是直接用PS的剪切工具,但发现有时不太好用. 由于你必须提前设好要剪切的尺寸. 也可能是我这小白不知道咋用. 下为摸索到的最好方法: 1.打开原图.新建图层,假设是png图片就不用 ...

  3. Hls平台实现sobel算法(一)

    索贝尔(Sobel)算子主要用于边缘检测,根据像素点的上下.左右邻点的灰度加权差与阈值进行比较,在边缘处达到极值的方法实现边缘检测. -------------序 一.原理性运行 流水线操作,将输入图 ...

  4. 深入理解js闭包【写的通俗易懂,很值的阅读】

    详细内容在下面这个链接里面: https://www.cnblogs.com/uedt/archive/2010/10/28/1863389.html 能写出这样的文章,定是大佬!

  5. Vue学习之旅:todomvc的学习练习

    一.前奏 1.todomvc官网地址:http://todomvc.com/ 查阅文档和下载插件都可以到这个官网上找. 2.上GitHub上搜索下载有人做的现成的本地模板:进入GitHub搜索todo ...

  6. 微信小程序wxss制作扭蛋机

    小程序制作扭蛋机 2019-09-24 13:26:53 公司要制作活动小程序,其中有一个扭蛋机的效果实现抽奖的功能.在网上找了好久竟没有找到(不知道是不是我找代码的方式有问题).最后还是自己做一个吧 ...

  7. C# ASP.NET发送电子邮件System.Net.Mail

    1.补充知识 (1)POP3和SMTP服务器是什么? 简单点来说:POP3 用于接收电子邮件 ,SMTP 用于发送电子邮件. (1)POP3具体指什么? POP3(Post Office Protoc ...

  8. Linux安装Sqoop及基础使用

    下载Sqoop 官网地址 http://sqoop.apache.org/ wget http://mirrors.hust.edu.cn/apache/sqoop/1.4.7/sqoop-1.4.7 ...

  9. 线程局部变量ThreadLocal实现原理

    ThreadLocal,即线程局部变量,用来为每一个使用它的线程维护一个独立的变量副本.这种变量只在线程的生命周期内有效.并且与锁机制那种以时间换取空间的做法不同,ThreadLocal没有任何锁机制 ...

  10. python3-返回函数

    函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个可变参数的求和.通常情况下,求和的函数是这样定义的: def calc_sum(*args): ax = ...