-
Notifications
You must be signed in to change notification settings - Fork 0
/
26.h
43 lines (36 loc) · 1.04 KB
/
26.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* @Author: FreedomLy
* @Date: 2018-04-26 09:01:43
* @Last Modified by: FreedomLy
* @Last Modified time: 2018-04-26 09:19:22
* 题目
* 输入一棵二叉树和一个整数,打印输出二叉树中节点值的和为输入整数的
* 所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路
* 径
*/
#pragma once
#include "TreeNode.h"
using tree::TreeNode;
class Solution26 {
public:
vector<vector<int>> find_path(TreeNode* root, int val)
{
vector<vector<int>> ret;
vector<int> path;
helper(root, ret, path, val);
return ret;
}
private:
// 递归求解
void helper(TreeNode* root, vector<vector<int>>& res, vector<int>& path, int sum)
{
if (!root) return;
path.emplace_back(root->val);
sum -= root->val;
if (sum == 0 && !root->left && !root->right)
res.emplace_back(path);
helper(root->left, res, path, sum);
helper(root->right, res, path, sum);
path.pop_back();
}
};