Last active
May 7, 2020 14:51
-
-
Save zeitan/07d67a7bb69d6cc3fe414a0c85b6069a to your computer and use it in GitHub Desktop.
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
private static boolean checkSumInTree(TreeNode root, int k) { | |
if (root != null) | |
return checkSumInTreeAcum(root, k, 0); | |
else | |
return false; | |
} |
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
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); | |
} |
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
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