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. mojing手柄遥杆控制

    using UnityEngine; using UnityEngine.UI; using System.Collections; using MojingSample.CrossPlatformI ...

  2. 国内Windows系统go get语言包

    这时候我们需要设置代理.代理工具我推荐用 lantern https://github.com/getlantern/lantern 需要注意的是,它的代理地址是: http://127.0.0.1: ...

  3. JAVA学习6:用Maven创建Spring3 MVC项目

    一.      环境 spring-framework-3.2.4.RELEASE jdk1.7.0_11 Maven3.0.5 eclipse-jee-juno-SR2-win32 二.      ...

  4. javaagent笔记及一个基于javassit的应用监控程序demo

    javaagent基本用法 定义入口premain public static void premain(String agentArgs, Instrumentation inst) { Syste ...

  5. HtmlUnit 开发网络爬虫

    网络爬虫第一个要面临的问题,就是如何抓取网页,抓取其实很容易,没你想的那么复杂,一个开源HtmlUnit包,几行代码就OK啦! 通常在一个页面中会包含别的Url,在别的Url当中又会包含更多的Url. ...

  6. mongodb数据库还原

    ./mongorestore -h -u myhuiqu -p Huiqu.com@ --authenticationDatabase /

  7. hadoop源码svn下载地址

    1.apache开源框架

  8. ubuntu中ANT的安装和配置

    一. 自动安装可以使用sudo apt-get install ant安装,但是这种装法不好.首先安装的ant不是最新的版本,其次还要装一堆其他的附带的东西.所以我才用自己手动ant安装. 二. 手动 ...

  9. 如何限制html标签input的长度

    如何限制html标签input的长度 示例: <form action="/example/html/form_action.asp" method="get&qu ...

  10. vue中echarts随窗体变化

    <div id="myChart" :style="{width: '100%', height: '345px'}"></div> & ...