Created
December 21, 2014 22:08
-
-
Save arjunrao87/92c31a4ec1dc2ca5adff to your computer and use it in GitHub Desktop.
Given a tree and a sum, return true if there is a path from the root down to a leaf, such that adding up all the values along the path equals the given sum.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean isSumExists( Node root, int sum ){ | |
if( root == null && sum != 0 ){ | |
return false; | |
} | |
int currentNodeSum = sum - root.getData(); | |
if( currentNodeSum == 0 ){ | |
return true; | |
} else{ | |
return ( isSumExists( root.getLeftChild(), currentNodeSum ) || isSumExists( root.getRightChild(), currentNodeSum ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment