path_sum
path_sum.cpp 源码
// 路径总和
#include "iostream"
#include "vector"
#include "queue"
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() {}
TreeNode(int val) : val(val) {}
TreeNode(int val, TreeNode *left, TreeNode *right) : val(val), left(left), right(right) {}
};
class Solution {
public:
// 深度优先
bool hasPathSum(TreeNode* root, int targetSum) {
if (!root) {
return false;
}
if (!root->left && !root->right) {
return targetSum == root->val;
}
int sub = targetSum - root->val;
return hasPathSum(root->left, sub) || hasPathSum(root->right, sub);
}
// 广度优先
bool hasPathSum2(TreeNode* root, int targetSum) {
if (!root) {
return false;
}
queue<TreeNode*> que;
que.push(root);
queue<int> vals;
vals.push(root->val);
while (!que.empty()) {
auto node = que.front();
que.pop();
int i = vals.front();
vals.pop();
if (!node->left && !node->right) {
if (targetSum == i) {
return true;
}
}
if (node->left) {
que.push(node->left);
vals.push(node->left->val + i);
}
if (node->right) {
que.push(node->right);
vals.push(node->right->val + i);
}
}
return false;
}
};
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦