From fbef70d7f8a1ba248dca0a80e791cbcb866f4dfb Mon Sep 17 00:00:00 2001 From: fartem Date: Fri, 4 Oct 2024 09:12:46 +0300 Subject: [PATCH] 2024-10-04 v. 6.7.5: added "236. Lowest Common Ancestor of a Binary Tree" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/common/binary_tree.rb | 8 ++ ...lowest_common_ancestor_of_a_binary_tree.rb | 15 ++++ ...lowest_common_ancestor_of_a_binary_tree.rb | 77 +++++++++++++++++++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 lib/medium/236_lowest_common_ancestor_of_a_binary_tree.rb create mode 100644 test/medium/test_236_lowest_common_ancestor_of_a_binary_tree.rb diff --git a/README.md b/README.md index ce1bb76d..64c8b7c1 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 8c7ace4d..cd0b9bad 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -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' diff --git a/lib/common/binary_tree.rb b/lib/common/binary_tree.rb index bea56b93..f46dcb3d 100644 --- a/lib/common/binary_tree.rb +++ b/lib/common/binary_tree.rb @@ -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 diff --git a/lib/medium/236_lowest_common_ancestor_of_a_binary_tree.rb b/lib/medium/236_lowest_common_ancestor_of_a_binary_tree.rb new file mode 100644 index 00000000..b8462647 --- /dev/null +++ b/lib/medium/236_lowest_common_ancestor_of_a_binary_tree.rb @@ -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 diff --git a/test/medium/test_236_lowest_common_ancestor_of_a_binary_tree.rb b/test/medium/test_236_lowest_common_ancestor_of_a_binary_tree.rb new file mode 100644 index 00000000..bfb477d9 --- /dev/null +++ b/test/medium/test_236_lowest_common_ancestor_of_a_binary_tree.rb @@ -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