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 @@ -591,3 +591,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 523. Continuous Subarray Sum | [Link](https://leetcode.com/problems/continuous-subarray-sum/) | [Link](./lib/medium/523_continuous_subarray_sum.rb) | [Link](./test/medium/test_523_continuous_subarray_sum.rb) |
| 524. Longest Word in Dictionary through Deleting | [Link](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/) | [Link](./lib/medium/524_longest_word_in_dictionary_through_deleting.rb) | [Link](./test/medium/test_524_longest_word_in_dictionary_through_deleting.rb) |
| 535. Encode and Decode TinyURL | [Link](https://leetcode.com/problems/encode-and-decode-tinyurl/) | [Link](./lib/medium/535_encode_and_decode_tinyurl.rb) | [Link](./test/medium/test_535_encode_and_decode_tinyurl.rb) |
| 538. Convert BST to Greater Tree | [Link](https://leetcode.com/problems/convert-bst-to-greater-tree/) | [Link](./lib/medium/538_convert_bst_to_greater_tree.rb) | [Link](./test/medium/test_538_convert_bst_to_greater_tree.rb) |
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 = '7.2.5'
s.version = '7.2.6'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
27 changes: 27 additions & 0 deletions lib/medium/538_convert_bst_to_greater_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

$sum = 0

# https://leetcode.com/problems/convert-bst-to-greater-tree/
# @param {TreeNode} root
# @return {TreeNode}
def convert_bst(root)
$sum = 0

convert_bst_with_sum(root)
end

private

# @param {TreeNode} node
# @return {TreeNode}
def convert_bst_with_sum(node)
return unless node

convert_bst_with_sum(node.right)
$sum += node.val
node.val = $sum
convert_bst_with_sum(node.left)

node
end
78 changes: 78 additions & 0 deletions test/medium/test_538_convert_bst_to_greater_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/common/binary_tree'
require_relative '../../lib/medium/538_convert_bst_to_greater_tree'
require 'minitest/autorun'

class ConvertBstToGreaterTreeTest < ::Minitest::Test
def test_default_one
assert(
::TreeNode.are_equals(
::TreeNode.new(
30,
::TreeNode.new(
36,
::TreeNode.new(36),
::TreeNode.new(
35,
nil,
::TreeNode.new(33)
)
),
::TreeNode.new(
21,
::TreeNode.new(26),
::TreeNode.new(
15,
nil,
::TreeNode.new(8)
)
)
),
convert_bst(
::TreeNode.new(
4,
::TreeNode.new(
1,
::TreeNode.new(0),
::TreeNode.new(
2,
nil,
::TreeNode.new(3)
)
),
::TreeNode.new(
6,
::TreeNode.new(5),
::TreeNode.new(
7,
nil,
::TreeNode.new(8)
)
)
)
)
)
)
end

def test_default_two
assert(
::TreeNode.are_equals(
::TreeNode.new(
1,
nil,
::TreeNode.new(1)
),
convert_bst(
::TreeNode.new(
0,
nil,
::TreeNode.new(1)
)
)
)
)
end
end
Loading