Skip to content

Commit c509c3f

Browse files
committed
2025-02-12 v. 8.5.2: added "1367. Linked List in Binary Tree"
1 parent 5f52a64 commit c509c3f

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
684684
| 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold | [Link](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/) | [Link](./lib/medium/1343_number_of_sub_arrays_of_size_k_and_average_greater_than_or_equal_to_threshold.rb) | [Link](./test/medium/test_1343_number_of_sub_arrays_of_size_k_and_average_greater_than_or_equal_to_threshold.rb) |
685685
| 1347. Minimum Number of Steps to Make Two Strings Anagram | [Link](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/) | [Link](./lib/medium/1347_minimum_number_of_steps_to_make_two_strings_anagram.rb) | [Link](./test/medium/test_1347_minimum_number_of_steps_to_make_two_strings_anagram.rb) |
686686
| 1358. Number of Substrings Containing All Three Characters | [Link](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/) | [Link](./lib/medium/1358_number_of_substrings_containing_all_three_characters.rb) | [Link](./test/medium/test_1358_number_of_substrings_containing_all_three_characters.rb) |
687+
| 1367. Linked List in Binary Tree | [Link](https://leetcode.com/problems/linked-list-in-binary-tree/) | [Link](./lib/medium/1367_linked_list_in_binary_tree.rb) | [Link](./test/medium/test_1367_linked_list_in_binary_tree.rb) |
687688
| 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) |
688689
| 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) |
689690
| 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) |

leetcode-ruby.gemspec

+1-1
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.5.1'
8+
s.version = '8.5.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/linked-list-in-binary-tree/
4+
# @param {ListNode} head
5+
# @param {TreeNode} root
6+
# @return {Boolean}
7+
def is_sub_path(head, root)
8+
nodes = [root]
9+
10+
until nodes.empty?
11+
node = nodes.shift
12+
13+
return true if node.val == head.val && dfs_for_sub_path(head, node)
14+
15+
nodes << node.left if node.left
16+
nodes << node.right if node.right
17+
end
18+
19+
false
20+
end
21+
22+
private
23+
24+
# @param {ListNode} head
25+
# @param {TreeNode} root
26+
# @return {Boolean}
27+
def dfs_for_sub_path(head, root)
28+
return true unless head
29+
30+
return false if root.nil? || root.val != head.val
31+
32+
left = dfs_for_sub_path(head.next, root.left)
33+
right = dfs_for_sub_path(head.next, root.right)
34+
35+
left || right
36+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/binary_tree'
5+
require_relative '../../lib/common/linked_list'
6+
require_relative '../../lib/medium/1367_linked_list_in_binary_tree'
7+
require 'minitest/autorun'
8+
9+
class LinkedListInBinaryTreeTest < ::Minitest::Test
10+
def test_default_one
11+
assert(
12+
is_sub_path(
13+
::ListNode.from_array(
14+
[4, 2, 8]
15+
),
16+
::TreeNode.build_tree(
17+
[1, 4, 4, nil, 2, 2, nil, 1, nil, 6, 8, nil, nil, nil, nil, 1, 3]
18+
)
19+
)
20+
)
21+
end
22+
23+
def test_default_two
24+
assert(
25+
is_sub_path(
26+
::ListNode.from_array(
27+
[1, 4, 2, 6]
28+
),
29+
::TreeNode.build_tree(
30+
[1, 4, 4, nil, 2, 2, nil, 1, nil, 6, 8, nil, nil, nil, nil, 1, 3]
31+
)
32+
)
33+
)
34+
end
35+
36+
def test_default_three
37+
assert(
38+
!is_sub_path(
39+
::ListNode.from_array(
40+
[1, 4, 2, 6, 8]
41+
),
42+
::TreeNode.build_tree(
43+
[1, 4, 4, nil, 2, 2, nil, 1, nil, 6, 8, nil, nil, nil, nil, 1, 3]
44+
)
45+
)
46+
)
47+
end
48+
end

0 commit comments

Comments
 (0)