MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / dfs

Method dfs

java/0112-path-sum.java:18–26  ·  view source on GitHub ↗
(TreeNode root, int targetSum, int currSum)

Source from the content-addressed store, hash-verified

16class Solution {
17 //This would be easily solved by DFS and then comparing the values
18 public boolean dfs(TreeNode root, int targetSum, int currSum){
19 if(root == null) return false;
20
21 currSum += root.val;
22 if(root.left == null && root.right == null){
23 return (currSum == targetSum);
24 }
25 return dfs(root.left, targetSum, currSum) || dfs(root.right, targetSum, currSum);
26 }
27 public boolean hasPathSum(TreeNode root, int targetSum) {
28 return dfs(root, targetSum, 0);
29 }

Callers 1

hasPathSumMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected