LinkSearchMenuExpandDocument

112. Path Sum

Solution Code

C

/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */boolhasPathSum(structTreeNode*root,inttargetSum){if(root==NULL)returnfalse;if(root->left==NULL&&root->right==NULL){if(root->val==targetSum)returntrue;returnfalse;}return(hasPathSum(root->left,targetSum-root->val)||hasPathSum(root->right,targetSum-root->val));}

© 2023. All rights reserved.

close