Created
November 9, 2020 23:02
-
-
Save brunofarache/fe958e399d40e20382f364431bf72be3 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
def maxAncestorDiff(root: TreeNode): Int = { | |
def visit(node: TreeNode, min: Int, max: Int): Int = { | |
if (node == null) return 0 | |
val diff = math.max(math.abs(node.value - max), math.abs(node.value - min)) | |
val newMin = math.min(node.value, min) | |
val newMax = math.max(node.value, max) | |
math.max(math.max(diff, visit(node.left, newMin, newMax)), visit(node.right, newMin, newMax)) | |
} | |
visit(root, root.value, root.value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment