Skip to content
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 @@ -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) |
Expand Down
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 = '8.3.1'
s.version = '8.3.2'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
30 changes: 30 additions & 0 deletions lib/medium/1123_lowest_common_ancestor_of_deepest_leaves.rb
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions test/medium/test_1123_lowest_common_ancestor_of_deepest_leaves.rb
Original file line number Diff line number Diff line change
@@ -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