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]
]
 
思路:简单的DFS。要注意的一点是,所有的路径必须到达叶子结点才算数。叶子结点的定义是没有孩子的节点。假如root节点没有孩子节点,则root节点就算是叶子结点。但当root节点有一个左孩子节点却没有右孩子节点时,它就不能算是叶子节点了。
 class Solution {
public:
void help(vector<vector<int> >& res, TreeNode* root, vector<int>& path, int target)
{
TreeNode *left = root->left, *right = root->right;
path.push_back(root->val);
if (!left && !right && target == root->val)
res.push_back(path);
if (left)
help(res, left, path, target - root->val);
if (right)
help(res, right, path, target - root->val);
path.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int> > res;
if (!root) return res;
vector<int> path;
help(res, root, path, sum);
return res;
}
};
 

Path Sum II (Find Path in Tree) -- LeetCode的更多相关文章

  1. [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 ...

  2. [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 ...

  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: 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】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 ...

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

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

  8. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

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

随机推荐

  1. OZ常见错误解决办法

    执行成功 错误信息解决办法 libvirt.libvirtError: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No ...

  2. disable-network-config

    network: {config: disabled}

  3. 用Python实现基于Hadoop Stream的mapreduce任务

    用Python实现基于Hadoop Stream的mapreduce任务 因为Hadoop Stream的存在,使得任何支持读写标准数据流的编程语言实现map和reduce操作成为了可能. 为了方便测 ...

  4. 【python基础】--常用数据结构

    list tuple dict set四种常用数据结构 list list 有序的集合,可以随时添加.删除其中元素值; 支持list嵌套模式, >>> p = ['a','b']&g ...

  5. 基于MAC OS 操作系统安装、配置hadoop

    在Mac上安装Hadoop 对我这个之前从未接触过*nix的用户来说,使用命令行来做一系列的事情还是废了一番功夫.特写这个记录,以做备份. 获取Java 我的Mac运行的操作系统是OS X 10.7 ...

  6. 第八篇:python基础_8 面向对象与网络编程

    本篇内容 接口与归一化设计 多态与多态性 封装 面向对象高级 异常处理 网络编程 一. 接口与归一化设计 1.定义 (1)归一化让使用者无需关心对象的类是什么,只需要知道这些对象都具备某些功能就可以了 ...

  7. Strassen优化矩阵乘法(复杂度O(n^lg7))

    按照算法导论写的 还没有测试复杂度到底怎么样 不过这个真的很卡内存,挖个坑,以后写空间优化 还有Matthew Anderson, Siddharth Barman写了一个关于矩阵乘法的论文 < ...

  8. Apache2.4启动时报AH00526错误(Invalid command 'Order')

    在WIN XP下手动配置PHP环境,安装Apache2.4+fastcgi后,重启Apache服务,出现如下提示: AH00526: Syntax error on line 293 of D:/ph ...

  9. 洛谷 P2041 分裂游戏 解题报告

    P2041 分裂游戏 题目描述 有一个无限大的棋盘,棋盘左下角有一个大小为 n 的阶梯形区域,其中最左下角的那个格子里有一枚棋子.你每次可以把一枚棋子"分裂"成两枚棋子,分别放在原 ...

  10. Glibc说明

    Glibc glibc是gnu发布的libc库,也即c运行库.glibc是linux系统中最底层的api(应用程序开发接口),几乎其它任何的运行库都会倚赖于glibc.glibc除了封装linux操作 ...