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

经典的深度优先算法

/**
* 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. js去掉输入框的前后空格及一些常用正则表达式

    去掉TextBox输入框两头的前后空格:onblur="this.value=this.value.replace(/^\s+|\s+$/g,'');" str为要去除空格的字符串 ...

  2. ORM数据库的增删改查

    数据库可视化工具: https://sqlitestudio.pl/index.rvt from app01 import models def orm(request): #增加数据 # 方法1: ...

  3. 攻防世界--re1-100

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/dc14f9a05f2846249336a84aecaf18a2.zip 1.准备 获取 ...

  4. Maven 集成Tomcat插件(引用)

    Maven已经是Java的项目管理标配,如何在JavaEE开发使用Maven调用Web应用,是很多同学关心的问题.本文将介绍,Maven如何介绍Tomcat插件. Maven Tomcat插件现在主要 ...

  5. 01.Windows2008R2系统禁启SMBv1服务命令

    微软漏洞安全问题: 检测:默认配置 = 已启用(未创建注册表项),所以不会返回 SMB1 值.Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\Lanm ...

  6. 脚本_查找 Linux 系统中的僵尸进程

    #!bin/bash#功能:查找Linux系统中的僵尸进程#作者:liusingbon#使用awk判断ps命令输出的第8列为Z时,显示该进程的 PID 和进程命令ps aux |awk '{if($8 ...

  7. Firewalld--03 富规则、备份恢复、开启内部上网

    目录 防火墙富规则.备份恢复.开启内部上网 1. 防火墙富规则策略 2.Firewalld备份恢复 3. 防火墙开启内部上网 防火墙富规则.备份恢复.开启内部上网 1. 防火墙富规则策略 ​ Fire ...

  8. Windows 进入上帝模式窗口

    Win10上帝模式如何启用? 默认情况下,Win10的上帝模式是隐藏的,如果要开启的话,操作步骤也非常简单,下面就介绍两种方法. 方法一.直接运行命令行 1.使用[Win + R ]快捷键打开“运行” ...

  9. vue2.0  之 directive指令 (自定义)

    指令 一.定义: 指令只一种可以附加到DOM元素的微命令(tiny commands). 它们通常以"v-"作为前缀, 以方便Vue知道你在使用一种特殊的标记, 从而确保语法的一致 ...

  10. CentOS7 利用systemctl添加自定义系统服务

    一.命令systemctl介绍 CentOS 7.0中已经没有service命令,而是启用了systemctl服务器命令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 命 ...