剑指34:二叉树中和为某一值的路径

传送门

nowcoder
leetcode

题目描述

输入一颗二叉树的根节点和一个整数,(按字典序)打印出二叉树中结点值的和为输入整数的所有路径。
路径定义为从树的根结点开始往下一直到叶结点所经过的结点。

C++ 代码 - nowcoder

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
递归实现,按照字典序返回结果
排序实现:lambda 表达式
*/
class Solution {
public:
vector<vector<int>> FindPath(TreeNode* root, int target) {
if (root == nullptr) return vector<vector<int>> {};

vector<vector<int>> res;
vector<int> path;
FindPathCore(res, path, root, target);
sort(res.begin(), res.end(), [](const vector<int>& a, const vector<int>& b) {
return a.size() > b.size();
});

return res;
}

void FindPathCore(vector<vector<int>>& res,
vector<int>& path,
TreeNode* node, int sum) {
if (node == nullptr) return;

path.push_back(node->val);
if (node->left == nullptr && node->right == nullptr && node->val == sum) {
res.push_back(path);
}

FindPathCore(res, path, node->left, sum - node->val);
FindPathCore(res, path, node->right, sum - node->val);
path.pop_back(); // 当前节点不满足要求则pop,返回其父节点
}
};

/*
递归实现,按照字典序返回结果
排序实现:重载 ()
*/
class Solution {
public:
struct Compare {
bool operator()(vector<int>& a, vector<int>& b) {
return a.size() > b.size();
}
};

vector<vector<int>> FindPath(TreeNode* root, int target) {
if (root == nullptr) return {};

vector<vector<int>> res;
vector<int> path;
FindPathCore(res, path, root, target);
sort(res.begin(), res.end(), Compare());

return res;
}

void FindPathCore(vector<vector<int>>& res,
vector<int> path,
TreeNode* node, int sum) {
if (node == nullptr) return;

path.push_back(node->val);
if (node->left == nullptr && node->right == nullptr && node->val == sum) {
res.push_back(path);
}

FindPathCore(res, path, node->left, sum - node->val);
FindPathCore(res, path, node->right, sum - node->val);
path.pop_back(); // 说明当前节点不满足要求则 pop,返回其父节点
}
};

C++ 代码 - leetcode

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
递归实现,未按照字典序返回结果
*/
class Solution {
public:
vector<vector<int>> pathTarget(TreeNode* root, int target) {
dfs(root, target);
return res;
}

void dfs(TreeNode* root, int target) {
if (root == nullptr) return;

path.emplace_back(root->val);
target -= root->val;
if (root->left == nullptr && root->right == nullptr && target == 0) {
res.emplace_back(path);
}

dfs(root->left, target);
dfs(root->right, target);
path.pop_back();
}

private:
vector<vector<int>> res;
vector<int> path;
};

/*
递归实现,按照字典序返回结果
*/
class Solution {
public:
vector<vector<int>> pathTarget(TreeNode* root, int target) {
if (!root) return {};

dfs(root, target);
sort(res.begin(), res.end(), [&](vector<int> v1, vector<int> v2) {
return v1.size() > v2.size();
});
return res;
}

void dfs(TreeNode* node, int target) {
if (node == nullptr) return;

path.push_back(node->val);
if (node->left == nullptr && node->right == nullptr && node->val == target) {
res.push_back(path);
}

dfs(node->left, target - node->val);
dfs(node->right, target - node->val);
path.pop_back();
}

private:
vector<vector<int>> res;
vector<int> path;
};

剑指34:二叉树中和为某一值的路径
https://lcf163.github.io/2021/01/31/剑指34:二叉树中和为某一值的路径/
作者
乘风的小站
发布于
2021年1月31日
许可协议