Skip to content

Instantly share code, notes, and snippets.

@evidanary
Created June 16, 2017 04:39
Show Gist options
  • Save evidanary/5c19df4501120b6c86908f9fe44051c1 to your computer and use it in GitHub Desktop.
Save evidanary/5c19df4501120b6c86908f9fe44051c1 to your computer and use it in GitHub Desktop.
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