diff --git a/README.md b/README.md index 80e1d028..775d705e 100644 --- a/README.md +++ b/README.md @@ -664,6 +664,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1081. Smallest Subsequence of Distinct Characters | [Link](https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/) | [Link](./lib/medium/1081_smallest_subsequence_of_distinct_characters.rb) | [Link](./test/medium/test_1081_smallest_subsequence_of_distinct_characters.rb) | | 1110. Delete Nodes And Return Forest | [Link](https://leetcode.com/problems/delete-nodes-and-return-forest/) | [Link](./lib/medium/1110_delete_nodes_and_return_forest.rb) | [Link](./test/medium/test_1110_delete_nodes_and_return_forest.rb) | | 1111. Maximum Nesting Depth of Two Valid Parentheses Strings | [Link](https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/) | [Link](./lib/medium/1111_maximum_nesting_depth_of_two_valid_parentheses_strings.rb) | [Link](./test/medium/test_1111_maximum_nesting_depth_of_two_valid_parentheses_strings.rb) | +| 1123. Lowest Common Ancestor of Deepest Leaves | [Link](https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/) | [Link](./lib/medium/1123_lowest_common_ancestor_of_deepest_leaves.rb) | [Link](./test/medium/test_1123_lowest_common_ancestor_of_deepest_leaves.rb) | | 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) | | 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) | | 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index ebc3cba1..af063668 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 = '8.3.1' + s.version = '8.3.2' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1123_lowest_common_ancestor_of_deepest_leaves.rb b/lib/medium/1123_lowest_common_ancestor_of_deepest_leaves.rb new file mode 100644 index 00000000..928b017c --- /dev/null +++ b/lib/medium/1123_lowest_common_ancestor_of_deepest_leaves.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/ +# @param {TreeNode} root +# @return {TreeNode} +def lca_deepest_leaves(root) + return unless root + + find_deepest_and_lca(root, 0)[1] +end + +private + +# @param {TreeNode} node +# @param {Integer} depth +# @return {TreeNode} +def find_deepest_and_lca(node, depth) + return [depth, node] unless node + + left_depth, left_lca = find_deepest_and_lca(node.left, depth + 1) + right_depth, right_lca = find_deepest_and_lca(node.right, depth + 1) + + if left_depth == right_depth + [left_depth, node] + elsif left_depth > right_depth + [left_depth, left_lca] + else + [right_depth, right_lca] + end +end diff --git a/test/medium/test_1123_lowest_common_ancestor_of_deepest_leaves.rb b/test/medium/test_1123_lowest_common_ancestor_of_deepest_leaves.rb new file mode 100644 index 00000000..b27c2569 --- /dev/null +++ b/test/medium/test_1123_lowest_common_ancestor_of_deepest_leaves.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/common/binary_tree' +require_relative '../../lib/medium/1123_lowest_common_ancestor_of_deepest_leaves' +require 'minitest/autorun' + +class LowestCommonAncestorOfDeepestLeavesTest < ::Minitest::Test + def test_default_one + assert( + ::TreeNode.are_equals( + lca_deepest_leaves( + ::TreeNode.build_tree( + [3, 5, 1, 6, 2, 0, 8, nil, nil, 7, 4] + ) + ), + ::TreeNode.build_tree( + [2, 7, 4] + ) + ) + ) + end + + def test_default_two + assert( + ::TreeNode.are_equals( + lca_deepest_leaves( + ::TreeNode.build_tree( + [1] + ) + ), + ::TreeNode.build_tree( + [1] + ) + ) + ) + end + + def test_default_three + assert( + ::TreeNode.are_equals( + lca_deepest_leaves( + ::TreeNode.build_tree( + [0, 1, 3, nil, 2] + ) + ), + ::TreeNode.build_tree( + [2] + ) + ) + ) + end +end