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. DB2 移动数据总结一

    数据移动参考的连接 IMPORT http://www-01.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/com.ibm.db2.luw.admin.cm ...

  2. C# CultureInfo中常用的InvariantCulture

    本文参考自CultureInfo中重要的InvariantCulture,纯属读书笔记,加深记忆 1.CultureInfo的InvariantCulture的作用 (1).CultureInfo使整 ...

  3. Java 二叉树一些基本操作

    求二叉树中节点个数: /*1. 求二叉树中的节点个数 递归解法: (1)如果二叉树为空,节点个数为0 (2)如果二叉树不为空,二叉树节点个数 = 左子树节点个数 + 右子树节点个数 + 1 */ pu ...

  4. js获取当前时间(昨天、今天、明天)

    开发过程中某些前台页面的时间控件我们需要给默认当前时间,jquery可以轻松的帮我们实现,代码如下 1 //昨天的时间 2 var day1 = new Date(); 3 day1.setTime( ...

  5. 配置文件详解和核心api讲解

    一.配置文件详解 1.映射文件详解 1.映射配置文件的位置和名称没有限制. -建议:位置:和实体类放在统一目录下.  名称:实体类名称.hbm.xml.    2.在映射配置文件中,标签内的name属 ...

  6. net 记录controller Action耗时

    可能有些时候需要记录Action的执行时间来优化系统功能,这时可以用过滤器来实现 第1个例子 using System; using System.Diagnostics; using System. ...

  7. sql语句将身份证号数字转换成特殊字符

    SELECT Tname , STUFF(Idcard,,,'*********') as Idcard,Completion from demo

  8. PHP学习10——Cookie和Session技术

    主要内容: Cookie技术 创建cookie 查看cookie 读取cookie 用cookie记录访问时间和次数 删除cookie cookie的生命周期 Session技术 session工作原 ...

  9. 跨平台 GUI可视化 网络调试工具

    mNetAssisthttp://blog.chinaunix.net/uid-21977056-id-4310527.htmlhttps://github.com/busyluo/mNetAssis ...

  10. 三、thymeleaf的使用

    1.简介 thymleaf是一个基于html的页面模板,springboot极力推荐使用它,代替jsp. API地址:https://www.thymeleaf.org/doc/tutorials/3 ...