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. 详解zabbix2.2.2安装部署(Server端篇)

    今天开始安装zabbix.zabbix需要LNMP或者LAMP环境.环境的搭建不在本章范围内. LNMP环境配置 Linux安装:http://www.osyunwei.com/archives/10 ...

  2. 自动化测试(三)如何用python写个双色球

    写一个程序,输入N就产生N条双色球号码 红球  6     01-33 蓝球  1     01-16 产生的双色球号码不能重复,写到一个文件里面,每一行是一条 红球: 01 03 05 07 08  ...

  3. 《Android权威编程指南(The Big Nerd Ranch Guide)(第二版)》12.4挑战练习

    本书第12章是讲解Dialog.12.4挑战练习是在CriminalIntent项目中,再增加一个TimePickerFragment的对话框fragment.通过在CrimeFragment用户界面 ...

  4. python之上下文管理、redis的发布订阅、rabbitmq

    使用with打开文件的方式,是调用了上下文管理的功能 #打开文件的两种方法: f = open('a.txt','r') with open('a.txt','r') as f 实现使用with关闭s ...

  5. Linux认知之旅【06 图形界面上的各种折腾】!

    玩linux免不了折腾,不折腾对不起linux 初次接触, 总会接触到绚丽的linux桌面! 但是随之而来的桌面优化,字体安装,操作习惯都需要一一适应

  6. 数据库——pymysql模块的使用(13)

    1.基本用法——建立链接,获取游标,执行sql语句,关闭 建立远程链接账号和权限 mysql> grant all on *.* to '; Query OK, rows affected, w ...

  7. 使用git和intelliJ

    intelliJ 官网创建账户之后Apply for a free student or teacher license for educational purposes就能免费使用专业版的intel ...

  8. swagger-ui enum select

    swagger-ui enum select Array[string] https://dejanstojanovic.net/aspnet/2018/march/displaying-multip ...

  9. [洛谷P2801]教主的魔法

    题目大意:有$n$个数,$q$个操作.两种操作: $M\;l\;r\;w:$把$[l,r]$所有数加上$w$ $A\;l\;r\;c:$查询$[l,r]$内大于等于$c$的元素的个数. 题解:分块,对 ...

  10. 自适应注意力机制在Image Caption中的应用

    在碎片化阅读充斥眼球的时代,越来越少的人会去关注每篇论文背后的探索和思考. 在这个栏目里,你会快速 get 每篇精选论文的亮点和痛点,时刻紧跟 AI 前沿成果. 点击本文底部的「阅读原文」即刻加入社区 ...