Skip to content

Instantly share code, notes, and snippets.

@arjunrao87
Created December 21, 2014 22:08
Show Gist options
  • Save arjunrao87/92c31a4ec1dc2ca5adff to your computer and use it in GitHub Desktop.
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.
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