MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / hasPathSum

Function hasPathSum

Trees/pathSum.cpp:8–15  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6 TreeNode *left,*right;
7};
8bool 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}

Callers 1

hasPathSumMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected