From 9bf37e1788d562a3a9dabb9430a86a3e0cb12140 Mon Sep 17 00:00:00 2001 From: fartem Date: Mon, 17 Mar 2025 08:16:35 +0300 Subject: [PATCH] 2025-03-14 v. 8.9.9: added "2096. Step-By-Step Directions From a Binary Tree Node to Another" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ...ions_from_a_binary_tree_node_to_another.rb | 56 +++++++++++++++++++ ...ions_from_a_binary_tree_node_to_another.rb | 34 +++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 lib/medium/2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb create mode 100644 test/medium/test_2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb diff --git a/README.md b/README.md index 91c343ab..51407117 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index d01dbb71..06b681bf 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -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' diff --git a/lib/medium/2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb b/lib/medium/2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb new file mode 100644 index 00000000..765b44e5 --- /dev/null +++ b/lib/medium/2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb @@ -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 diff --git a/test/medium/test_2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb b/test/medium/test_2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb new file mode 100644 index 00000000..6e5ce3c1 --- /dev/null +++ b/test/medium/test_2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb @@ -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