feat: 112_path_sum
This commit is contained in:
parent
f5f04b3783
commit
b883e27508
34
112_path_sum/main.cpp
Normal file
34
112_path_sum/main.cpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct TreeNode {
|
||||||
|
int val;
|
||||||
|
TreeNode *left;
|
||||||
|
TreeNode *right;
|
||||||
|
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool hasPathSum(TreeNode *root, int targetSum) {
|
||||||
|
unordered_map<TreeNode *, bool> visited;
|
||||||
|
|
||||||
|
function<bool(TreeNode *, int)> dfs = [&](TreeNode *root, int currentSum) -> bool {
|
||||||
|
if (!root) return false;
|
||||||
|
|
||||||
|
if (visited[root]) return false;
|
||||||
|
visited[root] = true;
|
||||||
|
currentSum += root->val;
|
||||||
|
|
||||||
|
if (!root->left && !root->right) {
|
||||||
|
return currentSum == targetSum;
|
||||||
|
}
|
||||||
|
|
||||||
|
return dfs(root->left, currentSum) || dfs(root->right, currentSum);
|
||||||
|
};
|
||||||
|
|
||||||
|
return dfs(root, 0);
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user