Created
June 16, 2017 04:39
-
-
Save evidanary/5c19df4501120b6c86908f9fe44051c1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 common_anc(root, p, q) | |
return root if (root.nil? || p == root.val || q == root.val) | |
left = common_anc(root.left, p, q) | |
right = common_anc(root.right, p, q) | |
left && right ? root : left || right | |
end | |
common_anc(head, 4, 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment