Skip to content

Commit 0d20cf1

Browse files
authored
Merge pull request #742 from fartem/236_Lowest_Common_Ancestor_of_a_Binary_Tree
2024-10-04 v. 6.7.5: added "236. Lowest Common Ancestor of a Binary Tree"
2 parents 0cd938b + fbef70d commit 0d20cf1

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,3 +540,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
540540
| 227. Basic Calculator II | [Link](https://leetcode.com/problems/basic-calculator-ii/) | [Link](./lib/medium/227_basic_calculator_ii.rb) | [Link](./test/medium/test_227_basic_calculator_ii.rb) |
541541
| 230. Kth Smallest Element in a BST | [Link](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | [Link](./lib/medium/230_kth_smallest_element_in_a_bst.rb) | [Link](./test/medium/test_230_kth_smallest_element_in_a_bst.rb) |
542542
| 235. Lowest Common Ancestor of a Binary Search Tree | [Link](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | [Link](./lib/medium/235_lowest_common_ancestor_of_a_binary_search_tree.rb) | [Link](./test/medium/test_235_lowest_common_ancestor_of_a_binary_search_tree.rb) |
543+
| 236. Lowest Common Ancestor of a Binary Tree | [Link](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | [Link](./lib/medium/236_lowest_common_ancestor_of_a_binary_tree.rb) | [Link](./test/medium/test_236_lowest_common_ancestor_of_a_binary_tree.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '6.7.4'
8+
s.version = '6.7.5'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/common/binary_tree.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ def self.are_equals(curr, other)
2525
right_eq = are_equals(curr.right, other.right)
2626
curr_eq && left_eq && right_eq
2727
end
28+
29+
# @param {TreeNode} other
30+
# @return {Boolean}
31+
def ==(other)
32+
return false unless other
33+
34+
val == other.val
35+
end
2836
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
4+
# @param {TreeNode} root
5+
# @param {TreeNode} p
6+
# @param {TreeNode} q
7+
# @return {TreeNode}
8+
def lowest_common_ancestor236(root, p, q)
9+
return root if [nil, p, q].index(root)
10+
11+
left = lowest_common_ancestor236(root.left, p, q)
12+
right = lowest_common_ancestor236(root.right, p, q)
13+
14+
left && right ? root : left || right
15+
end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/binary_tree'
5+
require_relative '../../lib/medium/236_lowest_common_ancestor_of_a_binary_tree'
6+
require 'minitest/autorun'
7+
8+
class LowestCommonAncestorOfABinaryTreeTest < ::Minitest::Test
9+
def test_default_one
10+
assert_equal(
11+
3,
12+
lowest_common_ancestor236(
13+
::TreeNode.new(
14+
3,
15+
::TreeNode.new(
16+
5,
17+
::TreeNode.new(6),
18+
::TreeNode.new(
19+
2,
20+
::TreeNode.new(7),
21+
::TreeNode.new(4)
22+
)
23+
),
24+
::TreeNode.new(
25+
1,
26+
::TreeNode.new(0),
27+
::TreeNode.new(8)
28+
)
29+
),
30+
::TreeNode.new(5),
31+
::TreeNode.new(1)
32+
).val
33+
)
34+
end
35+
36+
def test_default_two
37+
assert_equal(
38+
5,
39+
lowest_common_ancestor236(
40+
::TreeNode.new(
41+
3,
42+
::TreeNode.new(
43+
5,
44+
::TreeNode.new(6),
45+
::TreeNode.new(
46+
2,
47+
::TreeNode.new(7),
48+
::TreeNode.new(4)
49+
)
50+
),
51+
::TreeNode.new(
52+
1,
53+
::TreeNode.new(0),
54+
::TreeNode.new(8)
55+
)
56+
),
57+
::TreeNode.new(5),
58+
::TreeNode.new(4)
59+
).val
60+
)
61+
end
62+
63+
def test_default_three
64+
assert_equal(
65+
1,
66+
lowest_common_ancestor236(
67+
::TreeNode.new(
68+
1,
69+
::TreeNode.new(2),
70+
nil
71+
),
72+
::TreeNode.new(1),
73+
::TreeNode.new(2)
74+
).val
75+
)
76+
end
77+
end

0 commit comments

Comments
 (0)