113. Path Sum II (Tree; DFS)
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]
]
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) {
pathGroup.clear();
if(!root) return pathGroup; target = sum;
vector<int> path;
preOrder(root,,path);
return pathGroup; }
void preOrder(TreeNode* node,int sum,vector<int> path){
sum = node->val + sum;
path.push_back(node->val);
if(node->left)
{
preOrder(node->left,sum,path);
} if(node->right)
{
preOrder(node->right,sum,path);
} if(!node->left && !node->right)
{
if(sum==target)
{
pathGroup.push_back(path);
}
}
}
private:
int target;
vector<vector<int>> pathGroup;
};
113. Path Sum II (Tree; DFS)的更多相关文章
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- 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 ...
- 【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 ...
- Path Sum II 总结DFS
https://oj.leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf p ...
- [LeetCode] 113. Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- 113. Path Sum II
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- LeetCode 113. Path Sum II路径总和 II (C++)
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
随机推荐
- Linux之 iostat 解读磁盘io
1.iostat[oracle@orastb log]$ iostatLinux 3.10.0-327.el7.x86_64 (orastb.bonc.com.cn) 09/07/2017 _x86_ ...
- python 有关引用的一些问题
python 有关引用的一些问题 print id.__doc__ id(object) -> integer Return the identity of an object. This ...
- 数组游标实现对数组的各种操作(PHP学习)
如何不用foreach实现对数组实现循环? 答:我们只需要模拟foreach就行了,数组在执行foreach循环的时候,是有一个游标指向当前数组循环到的值, 那如果我们能拿到这个游标,并且操作游标,使 ...
- CRITICAL:yum.cli:Config Error: Error accessing file for config file:///etc/yum.conf
先试试yum install gcc , 1,下载最新的yum-3.2.28.tar.gz并解压 #wget http://yum.baseurl.org/download/3.2/yum-3.2. ...
- app的apk 安装的方法--adb--命令安装 (含把apk放某个文件夹,每次启动自己安装)
adb安装 1.在app自动化之前,首先手机上有要被测试的app,如何把电脑本地上的app安装到手机上呢?可以在运行自动化代码前,在cmd输入adb指令,把电脑app安装到手机上 adb instal ...
- PyQt 5布局管理
绝对定位 绝对定位有以下限制 1.如果调整窗口,控件的大小和位置不会改变 2.在各种平台上应用程序看起来不会一样 3.如果改变字体,我们的应用程序的布局就会改变 4.如果我们决定改变我们的布局,我们必 ...
- DBA_2PC_PENDING (转)
DBA_2PC_PENDINGOracle会自动处理分布事务,保证分布事务的一致性,所有站点全部提交或全部回滚.一般情况下,处理过程在很短的时间内完成,根本无法察觉到.但是,如果在commit或rol ...
- 查看进程id
#!/usr/bin/env python # encoding: utf-8 # Date: 2018/6/16 from multiprocessing import Processimport ...
- Vue语法
第一个Vue示例: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Jmeter如何监控服务器性能
1.jmeter只需要安装一些插件 ,就可以像loadrunner一样监控服务器CPU.内存等性能参数 1.下载需要的jmeter插件 如图上面两个是jmeter插件,可以再下面的链接中下载 ...