| 6 | TreeNode *left,*right; |
| 7 | }; |
| 8 | bool hasPathSum(TreeNode* root, int sum) { |
| 9 | if(!root) |
| 10 | return 0; |
| 11 | |
| 12 | if(!root->left && !root->right) // checks if the sum at the root is equal to target sum |
| 13 | return sum-root->val == 0; |
| 14 | return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val); // Traverses the tree to find the solution at one of the leaf nodes |
| 15 | } |