We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e895144 commit a416264Copy full SHA for a416264
ruby/236-Lowest-Common-Ancestor-of-a-Binary-Tree.rb
@@ -0,0 +1,10 @@
1
+def lowest_common_ancestor(root, p, q)
2
+ return root if root.val == p.val || root.val == q.val
3
+ return root if root.val.between?(p.val, q.val) || root.val.between?(q.val, p.val)
4
+
5
+ if root.val > p.val
6
+ lowest_common_ancestor(root.left, p, q)
7
+ else
8
+ lowest_common_ancestor(root.right, p, q)
9
+ end
10
+end
0 commit comments