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. pytorch 多GPU处理过程

    多GPU的处理机制: 使用多GPU时,pytorch的处理逻辑是: 1.在各个GPU上初始化模型. 2.前向传播时,把batch分配到各个GPU上进行计算. 3.得到的输出在主GPU上进行汇总,计算l ...

  2. TCP/TP:DNS区域(Zone)

    之前阅读资料不是特别明白,看到一个博主的解释,豁然开朗,特此记录. https://blog.csdn.net/huangzx3/article/details/79347556 DNS区域(ZONE ...

  3. Echarts 的简单使用

    http://echarts.baidu.com/index.html 直接用script引入从官网下载的echarts.js文件 官网的文件有几种版本的,按需下载即可,注意精简版的只显示折线.圆柱等 ...

  4. H5C3--background中cover,背景样式,提升响应区域+精灵图的使用

    一.cover的使用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  5. JavaScript 实例、构造函数、原型对象关系图

    详细介绍:深入理解javascript原型和闭包(5)——instanceof 图片来源:https://www.ibm.com/developerworks/cn/web/1306_jiangjj_ ...

  6. web端的兼容性测试

    目前主流的浏览器有:chrome.firefox.safari.IE edge.Opera等.其中IE edge ,Google浏览器 和firefox被称为现代浏览器. 浏览器排行榜2019年4月浏 ...

  7. 组合数学起步-排队[HNOI2012][BZOJ2729]

    <题面> 这个题十分基础 写这个博客给自己看的呵呵 遇到这个题,一看就是组合数学, so,开始推公式, 刚开始想的是,先排男生,再排女生,最后排老师 推了一会,呃呃呃,情况复杂,考虑的好像 ...

  8. MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; // 转换成多部分request异常

    原来是需要在springmvc中配置 <!-- multipartResolver上传文件 --> <bean id="multipartResolver" cl ...

  9. 文本流向 layout-flow

    作用与语法 文本流向layout-flow用来定义网页中的文本流向方式. 即排列方式,主要有两个属性,分别是horizonta (水平的) 和vertical-ideographic (垂直的). 文 ...

  10. CSS利用filter/opacity实现背景透明

    先看看众所周知的解决方案 如果我们想要一个半透明背景,有两种实现方式: 1.利用CSS和opacity属性 .opacity { filter:alpha(opacity=);/*IE浏览器*/ op ...