leetcode题解-112-路径总和 发表于 2019-12-25 题目 My way 递归实现 12345678910111213141516171819202122232425262728293031323334353637/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {private: void dfs(TreeNode* root, int& pathSum, int sum, bool& res) { if (!root || res) return ; pathSum += root->val; // 到达叶子结点时 if (!root->left && !root->right && pathSum == sum) res = true; dfs(root->left, pathSum, sum, res); dfs(root->right, pathSum, sum, res); pathSum -= root->val; }public: bool hasPathSum(TreeNode* root, int sum) { int pathSum = 0; bool res = false; dfs(root, pathSum, sum, res); return res; }};