112. Path Sum 發表於 2023-02-13 | 分類於 leetcode problemsolutionoption 1 - dfs123456789101112class Solution {public: bool hasPathSum(TreeNode* root, int targetSum) { // dfs // 終止條件 if(!root ) return false; targetSum-=root->val; // 拜訪到葉子了 if(!root->left && !root->right && targetSum == 0) return true; return hasPathSum(root->left, targetSum ) || hasPathSum(root->right, targetSum); }}; option 2 - bfs12345678910111213141516class Solution {public: bool hasPathSum(TreeNode* root, int targetSum) { if(!root) return false; queue<pair<TreeNode*,int>> q; q.push({root, root->val}); while(!q.empty()){ auto [p, val] = q.front(); q.pop(); if(!p->left && !p->right && val == targetSum) return true; if(p->left) q.push({p->left, p->left->val+val}); if(p->right) q.push({p->right, p->right->val + val}); } return false; }};