Skip to content

Commit

Permalink
236
Browse files Browse the repository at this point in the history
  • Loading branch information
TedTran2019 committed Jul 8, 2022
1 parent e895144 commit a416264
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ruby/236-Lowest-Common-Ancestor-of-a-Binary-Tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def lowest_common_ancestor(root, p, q)
return root if root.val == p.val || root.val == q.val
return root if root.val.between?(p.val, q.val) || root.val.between?(q.val, p.val)

if root.val > p.val
lowest_common_ancestor(root.left, p, q)
else
lowest_common_ancestor(root.right, p, q)
end
end

0 comments on commit a416264

Please sign in to comment.