Advertisement
vaibhav1906

Path sum

Jan 7th, 2022
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. class Solution {
  2. public:
  3.    
  4.     bool f(TreeNode* root, int targetSum){
  5.        
  6.         if(root==NULL)return false;
  7.        
  8.         if(root->left==NULL && root->right==NULL && targetSum == root->val){
  9.             return true;
  10.         }
  11.        
  12.         if(f(root->left,targetSum-root->val)==true || f(root->right,targetSum-root->val)==true)return true;
  13.        
  14.         return false;
  15.        
  16.     }
  17.    
  18.     bool hasPathSum(TreeNode* root, int targetSum) {
  19.        
  20.         return f(root,targetSum);
  21.        
  22.     }
  23. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement