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

解题:

实用递归的方法,题目很简单,每深一层递归,带入新计算出的需要求的sum值,递归函数需要四个入参:最终返回数组,当前已经经历的路径,当前需要的sum,已经结点指针

由于需要递归多个函数,刚开始为了防止“当前已经经历的路径”出现重复记载,因而没有形参没有使用指针,而完整的传入了整个数组,时间72ms

代码如下:

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int> > ret;
vector<int> cur_nums;
pathSumTree(ret, cur_nums, root, sum);
return ret;
} void pathSumTree(vector<vector<int> > &ret, vector<int> cur_nums, TreeNode *root, int cur_sum) {
if (!root)
return; cur_nums.push_back(root->val);
if (!root->left && !root->right && cur_sum - root->val == )
ret.push_back(cur_nums); pathSumTree(ret, cur_nums, root->left, cur_sum - root->val);
pathSumTree(ret, cur_nums, root->right, cur_sum - root->val);
return;
}
};

但是将形参设置为指针后,代码运行时间大大减少了,因为传递指针比传递整个数组高效多了;

为了防止出现混乱,可以在每次路径考察结束后,将当前的结点从数组中pop出来,避免重复;

代码如下,这次只需要不到20ms:

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int> > ret;
vector<int> cur_nums();
pathSumTree(ret, cur_nums, root, sum);
return ret;
} void pathSumTree(vector<vector<int> > &ret, vector<int> &cur_nums, TreeNode *root, int cur_sum) {
if (!root)
return; cur_nums.push_back(root->val);
if (!root->left && !root->right && cur_sum - root->val == ) {
ret.push_back(cur_nums);
cur_nums.pop_back();
return;
} pathSumTree(ret, cur_nums, root->left, cur_sum - root->val);
pathSumTree(ret, cur_nums, root->right, cur_sum - root->val);
cur_nums.pop_back();
return;
}
};

【Leetcode】【Medium】Path Sum II的更多相关文章

  1. 【leetcode】1289. Minimum Falling Path Sum II

    题目如下: Given a square grid of integers arr, a falling path with non-zero shifts is a choice of exactl ...

  2. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

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

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

  5. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  6. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  7. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

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

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

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

  10. Path Sum II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...

随机推荐

  1. 遇到Caused by: java.lang.NoClassDefFoundError: javax/validation/ParameterNameProvider

    今天在做spring和hibernate整合的时候遇到这个问题 网上搜找到这里有解决办法 http://blog.csdn.net/jueshengtianya/article/details/122 ...

  2. WPF的ProgressBar进度条

    1. ProgressBar常用属性 1.1.  Minimum:进度条的最小值,一般为 0 1.2. Maximum:进度条的最大值,一般为100 或者是 某一个数, 如复制文件时,总文件数等 1. ...

  3. Chrome Command API

    Chrome Command API 参考 Chrome开发工具已经强大得令人发指了,除了可通过UI操作的各种功能外,还提供了基于控制台的命令行方法,极大地方便了开发调试.现在就来领略下Chrome ...

  4. Vue路由开启keep-alive时的注意点

    Vue路由开启keep-alive时的注意点   这个不是业务的要求,但是看到每次进入页面就重新渲染DOM然后再获取数据更新DOM,觉得作为一个前端工程师有必要优化下的加载逻辑,正好vue提供了 ke ...

  5. Table Code

    post.PostToTags.Where(t => tagArray.Contains(t.PostTag.Name, comparerWihtoutCases) && !t. ...

  6. visual studio 安装与sqlserver 安装

    先sqlserver再visual studio ,避免安装目录重复(sqlserver 中包含一部分visual  而  visual studio 中也包含有部分sqlserver内容) 安装vi ...

  7. 2017年12月14日 LinQ高级查&&Asp.net WebForm Asp.net MVC

    LinQ的高级查询用法 开头:StartsWith()结尾:EndsWith()模糊:Contains() 个数:Count最大值:Max(r => r.price)最小值:Min(r => ...

  8. linux系统下部署项目

    一.修改防火墙设置,开放对应的端口 修改Linux系统防火墙配置需要修改 /etc/sysconfig/iptables 这个文件,如果要开放哪个端口,在里面添加一条  -A RH-Firewall- ...

  9. 执行shell脚本报错:syntax error near unexpected token `或syntax error: unexpected end of file

    引起此问题最可能的原因是: 在windows下编写的文件上传到linux执行. 我是在notepad++上编写的代码,之后上传到linux执行,报此错误.仔细检查,语法方面没有错误.上网查了一下,发现 ...

  10. JavaScript AMD 与CMD的代码区别

    1:CMD 依赖就近 define(function(require,export) { var b =1; var a = require("../a"); a.dosometh ...