Skip to content

Commit 614b01b

Browse files
authored
2025-01-29 v. 8.3.2: added "1123. Lowest Common Ancestor of Deepest Leaves"
2 parents 2505c78 + e8705a6 commit 614b01b

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
664664
| 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) |
665665
| 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) |
666666
| 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) |
667+
| 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) |
667668
| 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) |
668669
| 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) |
669670
| 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) |

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 = '8.3.1'
8+
s.version = '8.3.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/
4+
# @param {TreeNode} root
5+
# @return {TreeNode}
6+
def lca_deepest_leaves(root)
7+
return unless root
8+
9+
find_deepest_and_lca(root, 0)[1]
10+
end
11+
12+
private
13+
14+
# @param {TreeNode} node
15+
# @param {Integer} depth
16+
# @return {TreeNode}
17+
def find_deepest_and_lca(node, depth)
18+
return [depth, node] unless node
19+
20+
left_depth, left_lca = find_deepest_and_lca(node.left, depth + 1)
21+
right_depth, right_lca = find_deepest_and_lca(node.right, depth + 1)
22+
23+
if left_depth == right_depth
24+
[left_depth, node]
25+
elsif left_depth > right_depth
26+
[left_depth, left_lca]
27+
else
28+
[right_depth, right_lca]
29+
end
30+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/binary_tree'
5+
require_relative '../../lib/medium/1123_lowest_common_ancestor_of_deepest_leaves'
6+
require 'minitest/autorun'
7+
8+
class LowestCommonAncestorOfDeepestLeavesTest < ::Minitest::Test
9+
def test_default_one
10+
assert(
11+
::TreeNode.are_equals(
12+
lca_deepest_leaves(
13+
::TreeNode.build_tree(
14+
[3, 5, 1, 6, 2, 0, 8, nil, nil, 7, 4]
15+
)
16+
),
17+
::TreeNode.build_tree(
18+
[2, 7, 4]
19+
)
20+
)
21+
)
22+
end
23+
24+
def test_default_two
25+
assert(
26+
::TreeNode.are_equals(
27+
lca_deepest_leaves(
28+
::TreeNode.build_tree(
29+
[1]
30+
)
31+
),
32+
::TreeNode.build_tree(
33+
[1]
34+
)
35+
)
36+
)
37+
end
38+
39+
def test_default_three
40+
assert(
41+
::TreeNode.are_equals(
42+
lca_deepest_leaves(
43+
::TreeNode.build_tree(
44+
[0, 1, 3, nil, 2]
45+
)
46+
),
47+
::TreeNode.build_tree(
48+
[2]
49+
)
50+
)
51+
)
52+
end
53+
end

0 commit comments

Comments
 (0)