Skip to content

Commit 6a31504

Browse files
authored
2025-03-14 v. 8.9.9: added "2096. Step-By-Step Directions From a Binary Tree Node to Another"
2 parents 8dc9018 + 9bf37e1 commit 6a31504

4 files changed

+92
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
732732
| 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) |
733733
| 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) |
734734
| 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) |
735+
| 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) |
735736
| 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) |
736737
| 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) |
737738
| 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) |

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.9.8'
8+
s.version = '8.9.9'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/
4+
# @param {TreeNode} root
5+
# @param {Integer} start_value
6+
# @param {Integer} dest_value
7+
# @return {String}
8+
def get_directions(root, start_value, dest_value)
9+
find_path =
10+
lambda { |node, target, path|
11+
return false if node.nil?
12+
13+
path << node
14+
15+
return true if node.val == target
16+
17+
return true if find_path.call(node.left, target, path) || find_path.call(node.right, target, path)
18+
19+
path.pop
20+
21+
false
22+
}
23+
24+
start_path = []
25+
dest_path = []
26+
27+
find_path.call(root, start_value, start_path)
28+
find_path.call(root, dest_value, dest_path)
29+
30+
lca = nil
31+
[start_path.size, dest_path.size].min.times do |i|
32+
break unless start_path[i] == dest_path[i]
33+
34+
lca = start_path[i]
35+
end
36+
37+
start_to_lca = []
38+
lca_to_dest = []
39+
40+
start_path.reverse.each_with_index do |node, _i|
41+
break if node == lca
42+
43+
start_to_lca << 'U'
44+
end
45+
46+
lca_index = dest_path.index(lca)
47+
(lca_index...dest_path.size - 1).each do |i|
48+
lca_to_dest << if dest_path[i].left == dest_path[i + 1]
49+
'L'
50+
else
51+
'R'
52+
end
53+
end
54+
55+
(start_to_lca + lca_to_dest).join
56+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/binary_tree'
5+
require_relative '../../lib/medium/2096_step_by_step_directions_from_a_binary_tree_node_to_another'
6+
require 'minitest/autorun'
7+
8+
class StepByStepDirectionsFromABinaryTreeNodeToAnotherTest < ::Minitest::Test
9+
def test_default_one
10+
assert_equal(
11+
'UURL',
12+
get_directions(
13+
::TreeNode.build_tree(
14+
[5, 1, 2, 3, nil, 6, 4]
15+
),
16+
3,
17+
6
18+
)
19+
)
20+
end
21+
22+
def test_default_two
23+
assert_equal(
24+
'L',
25+
get_directions(
26+
::TreeNode.build_tree(
27+
[2, 1]
28+
),
29+
2,
30+
1
31+
)
32+
)
33+
end
34+
end

0 commit comments

Comments
 (0)