LeetCode_Path Sum II
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]
]
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void DFS(TreeNode *root, vector<int> &temp, int tempSum)
{
if(root->left == NULL && root -> right== NULL && tempSum == sum) {
result.push_back(temp);
return;
} if(root->left!= NULL ){
temp.push_back(root->left->val);
DFS(root->left, temp,root->left->val + tempSum );
temp.pop_back();
} if(root->right != NULL ){
temp.push_back(root->right->val);
DFS(root->right, temp, root->right->val + tempSum);
temp.pop_back();
}
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
result.clear();
if(!root) return result;
this->sum = sum;
vector<int> temp;
temp.push_back(root->val);
DFS(root, temp, root->val);
return result;
}
private:
int sum;
vector<vector<int>> result;
};
LeetCode_Path Sum II的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- 【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 ...
- 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 ...
- 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 ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- leetcode2 Two Sum II – Input array is sorted
Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...
随机推荐
- C#进程间通讯技术-整理。
原文:C#进程间通讯技术-整理. 扩展阅读:http://www.cnblogs.com/joye-shen/archive/2012/06/16/2551864.html 一.进程间通讯的方式 1) ...
- Cmake ,Out of Source Build
Out of Source build呢,就是让Cmake产生的临时垃圾文件,不关乎于项目实际本身的文件放到一个目录里,一般我们把这个目录放在项目根目录(也可以认为是根CmakeLists.txt)下 ...
- 深入浅出Node.js (7) - 网络编程
7.1 构建TCP服务 7.1.1 TCP 7.1.2 创建TCP服务器端 7.1.3 TCP服务的事件 7.2 构建UDP服务 7.2.1 创建UDP套接字 7.2.2 创建UDP服务器端 7.2. ...
- bzoj 1195
http://www.lydsy.com/JudgeOnline/problem.php?id=1195 状压DP. 首先去掉被包含的字符串. 对于字符串i和j,我们求出 当字符串j的左端点在字符串i ...
- 深入理解linux网络技术内幕读书笔记(四)--通知链
Table of Contents 1 概述 2 定义链 3 链注册 4 链上的通知事件 5 网络子系统的通知链 5.1 包裹函数 5.2 范例 6 测试实例 概述 [注意] 通知链只在内核子系统之间 ...
- CSS的基本认识
1.定义: 级联样式表(Cascading Style Sheet)简称“CSS”,通常又称为“风格样式表(Style Sheet)”,它是用来进行网页风格设计的. 2.对CSS的基本认识: CSS是 ...
- 自制USB wifi信号放大天线
这是我的usb wifi天线第一个版本,灵感来自: http://www.instructables.com/id/EQARE4I72GEPUCHTHU/ http://www.usbwifi.orc ...
- hdu 5402 Travelling Salesman Problem(大模拟)
Problem Description Teacher Mai ,) to the bottom right corner (n,m). He can choose one direction and ...
- ubuntu14.04 + cocos2d-x-3.6 + eclipse发布android
cocos2d-x-2.2.6版本 :http://www.cnblogs.com/weishuan/p/4698470.html 接下来是3.6了 ,准备好下面四个东东,我把这些都放在XXX/App ...
- C# 约瑟夫环算法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...