Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024-10-04 v. 6.7.5: added "236. Lowest Common Ancestor of a Binary Tree" #742

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '6.7.4'
s.version = '6.7.5'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
8 changes: 8 additions & 0 deletions lib/common/binary_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ def self.are_equals(curr, other)
right_eq = are_equals(curr.right, other.right)
curr_eq && left_eq && right_eq
end

# @param {TreeNode} other
# @return {Boolean}
def ==(other)
return false unless other

val == other.val
end
end
15 changes: 15 additions & 0 deletions lib/medium/236_lowest_common_ancestor_of_a_binary_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
# @param {TreeNode} root
# @param {TreeNode} p
# @param {TreeNode} q
# @return {TreeNode}
def lowest_common_ancestor236(root, p, q)
return root if [nil, p, q].index(root)

left = lowest_common_ancestor236(root.left, p, q)
right = lowest_common_ancestor236(root.right, p, q)

left && right ? root : left || right
end
77 changes: 77 additions & 0 deletions test/medium/test_236_lowest_common_ancestor_of_a_binary_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/common/binary_tree'
require_relative '../../lib/medium/236_lowest_common_ancestor_of_a_binary_tree'
require 'minitest/autorun'

class LowestCommonAncestorOfABinaryTreeTest < ::Minitest::Test
def test_default_one
assert_equal(
3,
lowest_common_ancestor236(
::TreeNode.new(
3,
::TreeNode.new(
5,
::TreeNode.new(6),
::TreeNode.new(
2,
::TreeNode.new(7),
::TreeNode.new(4)
)
),
::TreeNode.new(
1,
::TreeNode.new(0),
::TreeNode.new(8)
)
),
::TreeNode.new(5),
::TreeNode.new(1)
).val
)
end

def test_default_two
assert_equal(
5,
lowest_common_ancestor236(
::TreeNode.new(
3,
::TreeNode.new(
5,
::TreeNode.new(6),
::TreeNode.new(
2,
::TreeNode.new(7),
::TreeNode.new(4)
)
),
::TreeNode.new(
1,
::TreeNode.new(0),
::TreeNode.new(8)
)
),
::TreeNode.new(5),
::TreeNode.new(4)
).val
)
end

def test_default_three
assert_equal(
1,
lowest_common_ancestor236(
::TreeNode.new(
1,
::TreeNode.new(2),
nil
),
::TreeNode.new(1),
::TreeNode.new(2)
).val
)
end
end
Loading