Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.(Medium)

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

分析:

回溯法处理即可,利用helper函数遍历所有路径,达到sum时添加到result里。

代码:

 /**
* 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 {
vector<vector<int>> result;
void helper(TreeNode* root, int sum, vector<int>& vec) {
if (root == nullptr) {
return;
}
vec.push_back(root -> val);
sum -= root -> val;
if (root -> left == nullptr && root -> right == nullptr) {
if (sum == ) {
result.push_back(vec);
}
else {
vec.pop_back();
sum += root -> val;
return;
}
}
if (root -> left != nullptr) {
helper(root -> left, sum, vec);
}
if (root -> right != nullptr) {
helper(root -> right, sum, vec);
}
vec.pop_back();
sum -= root -> val;
}
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<int> vec;
helper(root, sum, vec);
return result;
}
};

LeetCode113 Path Sum II的更多相关文章

  1. 第34-2题:LeetCode113. Path Sum II

    题目 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ ...

  2. Leetcode113. Path Sum II路径总和2

    给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 ...

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

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

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

  7. 32. Path Sum && Path Sum II

    Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...

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

  9. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

随机推荐

  1. Broken Keyboard UVA 11988 数组实现链表

    这个构造十分巧妙,,,又学到一招,有点类似数组实现的邻接表 #include <iostream> #include <string.h> #include <cstdi ...

  2. leetcode 665

    665. Non-decreasing Array Input: [4,2,3] Output: True Explanation: You could modify the first 4 to 1 ...

  3. freopen() 函数的使用

    当我们求解acm题目时,通常在设计好算法和程序后,要在调试环境(例如VC等)中运行程序,输入测试数据,当能得到正确运行结果后,才将程序提交到oj中.但由于调试往往不能一次成功,每次运行时,都要重新输入 ...

  4. java swing调试时线程显示名字

    一般有一个默认名字 但是具体运行到哪一个线程,需要猜 为了节约时间,提高效率 可以给线程写个中文名(因为默认就是英文,写中文,一眼就能挑出来) 以RTC定时器为例子 final TimerRtc ti ...

  5. 关于python的字典操作

    字典和列表的区别: 列表是有序的 字典是无序的 字典使用{}定义 字典使用键值对存储数据,键值对之间使用 “   ,”分隔 键 key 是索引 值 value 是数据 键和值之间使用  “  :”分隔 ...

  6. 关系数据库标准语言 SQL (ch.3)

    3.1 SQL 概述 3.1.2 特点 1 综合统一 非关系型语言 的数据语言都分为 DDL Scheme Data Definitin Language, 模式DDL SubScheme Data ...

  7. Xcode8遇到的问题及解决方案!!!

    http://blog.csdn.net/jnbbwyth/article/details/52576169 http://www.cocoachina.com/ios/20161227/18451. ...

  8. Leetcode12.Integer to Roman整数转罗马数字

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...

  9. Eclipse 的 Java Web 项目环境搭建

    从svn上拉取下来Eclipse的项目 IntelliJ IDEA自动识别到可编译的 src 类目录 Java Web 项目 html(一般命名为:WebRoot) 是整个项目输出的根目录. WEB- ...

  10. 一个宽度不确定的DIV里放三个水平对齐的DIV,左右两个DIV宽度固定为100px,中间那个DIV自适应宽度

    方法一:浮动  注意三个div的位置 <html><head> <meta charset="utf-8"> <style type=&q ...