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

2025-02-04 v. 8.4.1: added "1302. Deepest Leaves Sum" #930

Merged
merged 1 commit into from
Feb 3, 2025
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 @@ -673,6 +673,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 1249. Minimum Remove to Make Valid Parentheses | [Link](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [Link](./lib/medium/1249_minimum_remove_to_make_valid_parentheses.rb) | [Link](./test/medium/test_1249_minimum_remove_to_make_valid_parentheses.rb) |
| 1261. Find Elements in a Contaminated Binary Tree | [Link](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/) | [Link](./lib/medium/1261_find_elements_in_a_contaminated_binary_tree.rb) | [Link](./test/medium/test_1261_find_elements_in_a_contaminated_binary_tree.rb) |
| 1288. Remove Covered Intervals | [Link](https://leetcode.com/problems/remove-covered-intervals/) | [Link](./lib/medium/1288_remove_covered_intervals.rb) | [Link](./test/medium/test_1288_remove_covered_intervals.rb) |
| 1302. Deepest Leaves Sum | [Link](https://leetcode.com/problems/deepest-leaves-sum/) | [Link](./lib/medium/1302_deepest_leaves_sum.rb) | [Link](./test/medium/test_1302_deepest_leaves_sum.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.4.0'
s.version = '8.4.1'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
38 changes: 38 additions & 0 deletions lib/medium/1302_deepest_leaves_sum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

# https://leetcode.com/problems/deepest-leaves-sum/
# @param {TreeNode} root
# @return {Integer}
def deepest_leaves_sum(root)
@max_deep = max_deep(root, 0)

traverse_for_deepest_leaves_sum(root, 0)
end

private

# @param {TreeNode} node
# @param {Integer} curr
# @return {Integer}
def max_deep(node, curr)
return curr unless node

left = max_deep(node.left, curr + 1)
right = max_deep(node.right, curr + 1)

[left, right].max
end

# @param {TreeNode} node
# @param {Integer} curr
# @return {Integer}
def traverse_for_deepest_leaves_sum(node, curr)
return 0 unless node

return node.val if curr + 1 == @max_deep

left = traverse_for_deepest_leaves_sum(node.left, curr + 1)
right = traverse_for_deepest_leaves_sum(node.right, curr + 1)

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

require_relative '../test_helper'
require_relative '../../lib/common/binary_tree'
require_relative '../../lib/medium/1302_deepest_leaves_sum'
require 'minitest/autorun'

class DeepestLeavesSumTest < ::Minitest::Test
def test_case_one
assert_equal(
15,
deepest_leaves_sum(
::TreeNode.build_tree(
[1, 2, 3, 4, 5, nil, 6, 7, nil, nil, nil, nil, 8]
)
)
)
end

def test_case_two
assert_equal(
19,
deepest_leaves_sum(
::TreeNode.build_tree(
[6, 7, 8, 2, 7, 1, 3, 9, nil, 1, 4, nil, nil, nil, 5]
)
)
)
end
end