Skip to content

Instantly share code, notes, and snippets.

@zeitan
Last active May 7, 2020 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeitan/07d67a7bb69d6cc3fe414a0c85b6069a to your computer and use it in GitHub Desktop.
Save zeitan/07d67a7bb69d6cc3fe414a0c85b6069a to your computer and use it in GitHub Desktop.
private static boolean checkSumInTree(TreeNode root, int k) {
if (root != null)
return checkSumInTreeAcum(root, k, 0);
else
return false;
}
private static boolean checkSumInTree(TreeNode node, int k, int acum) {
if (node == null )
return false;
if (isLeaf(node) && acum + node.val == k)
return true;
else
if (acum + node.val > k)
return false;
else
return checkSumInTreeAcum(node.left, k, acum + node.val) || checkSumInTreeAcum(node.right, k, acum + node.val);
}
private static boolean isLeaf(TreeNode node) {
return (node != null && node.right == null && node.left == null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment