Skip to content

2025-03-14 v. 8.9.9: added "2096. Step-By-Step Directions From a Binary Tree Node to Another" #988

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

Merged
Merged
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
@@ -732,6 +732,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 2074. Reverse Nodes in Even Length Groups | [Link](https://leetcode.com/problems/reverse-nodes-in-even-length-groups/) | [Link](./lib/medium/2074_reverse_nodes_in_even_length_groups.rb) | [Link](./test/medium/test_2074_reverse_nodes_in_even_length_groups.rb) |
| 2091. Removing Minimum and Maximum From Array | [Link](https://leetcode.com/problems/removing-minimum-and-maximum-from-array/) | [Link](./lib/medium/2091_removing_minimum_and_maximum_from_array.rb) | [Link](./test/medium/test_2091_removing_minimum_and_maximum_from_array.rb) |
| 2095. Delete the Middle Node of a Linked List | [Link](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/) | [Link](./lib/medium/2095_delete_the_middle_node_of_a_linked_list.rb) | [Link](./test/medium/test_2095_delete_the_middle_node_of_a_linked_list.rb) |
| 2096. Step-By-Step Directions From a Binary Tree Node to Another | [Link](https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/) | [Link](./lib/medium/2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb) | [Link](./test/medium/test_2096_step_by_step_directions_from_a_binary_tree_node_to_another.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) |
| 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '8.9.8'
s.version = '8.9.9'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

# https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/
# @param {TreeNode} root
# @param {Integer} start_value
# @param {Integer} dest_value
# @return {String}
def get_directions(root, start_value, dest_value)
find_path =
lambda { |node, target, path|
return false if node.nil?

path << node

return true if node.val == target

return true if find_path.call(node.left, target, path) || find_path.call(node.right, target, path)

path.pop

false
}

start_path = []
dest_path = []

find_path.call(root, start_value, start_path)
find_path.call(root, dest_value, dest_path)

lca = nil
[start_path.size, dest_path.size].min.times do |i|
break unless start_path[i] == dest_path[i]

lca = start_path[i]
end

start_to_lca = []
lca_to_dest = []

start_path.reverse.each_with_index do |node, _i|
break if node == lca

start_to_lca << 'U'
end

lca_index = dest_path.index(lca)
(lca_index...dest_path.size - 1).each do |i|
lca_to_dest << if dest_path[i].left == dest_path[i + 1]
'L'
else
'R'
end
end

(start_to_lca + lca_to_dest).join
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/common/binary_tree'
require_relative '../../lib/medium/2096_step_by_step_directions_from_a_binary_tree_node_to_another'
require 'minitest/autorun'

class StepByStepDirectionsFromABinaryTreeNodeToAnotherTest < ::Minitest::Test
def test_default_one
assert_equal(
'UURL',
get_directions(
::TreeNode.build_tree(
[5, 1, 2, 3, nil, 6, 4]
),
3,
6
)
)
end

def test_default_two
assert_equal(
'L',
get_directions(
::TreeNode.build_tree(
[2, 1]
),
2,
1
)
)
end
end