/** * 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 { public: bool hasPathSum(TreeNode* root, int sum) { if(root == NULL) return false; int flag = 0; DFS(root,sum,flag); if(flag) return true; else return false; } void DFS(TreeNode* root, int sum,int& flag){ if((root->left == NULL)&&(root->right == NULL)){ if(root->val == sum) flag = 1; } else if((root->left != NULL)&&(root->right == NULL)){ root->left->val += root->val; DFS(root->left,sum,flag); } else if((root->left == NULL)&&(root->right != NULL)){ root->right->val += root->val; DFS(root->right,sum,flag); } else if((root->left != NULL)&&(root->right != NULL)){ root->left->val += root->val; DFS(root->left,sum,flag); root->right->val += root->val; DFS(root->right,sum,flag); } return; } };
__
简便写法:
class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(!root) return false; if(root->left == NULL && root->right == NULL) return root->val == sum; return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); //sum - root->val很灵性 } };