###### 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]
]


vector<vector<int>> pathSum(TreeNode* root, int sum) {} 这是我们需要实现的功能函数

 <span style="white-space:pre">	</span>vector<vector<int>> pathsleft;
vector<vector<int>> pathsright;
if(root->left) pathsleft = pathSum(root->left,sum-root->val);
if(root->right) pathsright = pathSum(root->right,sum-root->val);
for(auto p:pathsleft){
p.insert(p.begin(),root->val);
paths.push_back(p);
}
for(auto p:pathsright){
p.insert(p.begin(),root->val);
paths.push_back(p);
}

        if(!root->left && !root->right){
if(sum==root->val){
vector<int> t;
t.push_back(sum);
paths.push_back(t);
}
return paths;
}

/**
* 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 {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> paths;
if(!root) return paths;
if(!root->left && !root->right){
if(sum==root->val){
vector<int> t;
t.push_back(sum);
paths.push_back(t);
}
return paths;
}
vector<vector<int>> pathsleft;
vector<vector<int>> pathsright;
if(root->left) pathsleft = pathSum(root->left,sum-root->val);
if(root->right) pathsright = pathSum(root->right,sum-root->val);
for(auto p:pathsleft){
p.insert(p.begin(),root->val);
paths.push_back(p);
}
for(auto p:pathsright){
p.insert(p.begin(),root->val);
paths.push_back(p);
}
return paths;
}
};

<span style="font-family:Times New Roman;">  vector<vector<int>> paths;
vector<vector<int>> pathsleft;
vector<vector<int>> pathsright;</span>

void FindPaths(TreeNode* root,int sum,vector<int>& path,vector<vector<int>>& paths)

paths只会在找到一条符合条件的路径path时才会push_back(path);其余时候，paths不会改变。

    void FindPaths(TreeNode* root,int sum,vector<int>& path,vector<vector<int>>& paths){
if(!root) return;
path.push_back(root->val);
if(root->val==sum && !root->left && !root->right){
paths.push_back(path);
}
FindPaths(root->left,sum-root->val,path,paths);
FindPaths(root->right,sum-root->val,path,paths);
path.pop_back();
}

/**
* 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 {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> paths;
vector<int> path;
FindPaths(root,sum,path,paths);
return paths;
}
void FindPaths(TreeNode* root,int sum,vector<int>& path,vector<vector<int>>& paths){
if(!root) return;
path.push_back(root->val);
if(root->val==sum && !root->left && !root->right){
paths.push_back(path);
}
FindPaths(root->left,sum-root->val,path,paths);
FindPaths(root->right,sum-root->val,path,paths);
path.pop_back();
}
};

#### Path Sum II -- LeetCode

2014-04-14 23:59:49

#### 【leetcode c++】113 Path Sum II

2015-08-01 00:06:17

#### 【LeetCode-面试算法经典-Java实现】【113-Path Sum II（路径和）】

2015-08-12 06:23:36

#### 【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

2016-05-09 22:25:36

#### leetcode 113. Path Sum II-路径和|回溯算法

2016-06-11 15:35:35

#### 【leetCode】Path Sum II python实现

2016-06-18 21:54:06

#### 【LeetCode with Python】 Path Sum II

2014-06-27 18:02:30

#### leetcode_c++:树: Path SumII（113）

2016-08-25 16:48:12

#### LeetCode详解 之 Path Sum I and II(Java)

2015-05-24 00:55:29

#### LeetCode 113. Path Sum II（路径和）

2016-05-24 00:19:11

## 不良信息举报

LeetCode题解——Path Sum II