Skip to content

Commit 086b1ec

Browse files
committed
2025-02-19 v. 8.6.2: added "1457. Pseudo-Palindromic Paths in a Binary Tree"
1 parent 0c12922 commit 086b1ec

4 files changed

+66
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
695695
| 1433. Check If a String Can Break Another String | [Link](https://leetcode.com/problems/check-if-a-string-can-break-another-string/) | [Link](./lib/medium/1433_check_if_a_string_can_break_another_string.rb) | [Link](./test/medium/test_1433_check_if_a_string_can_break_another_string.rb) |
696696
| 1442. Count Triplets That Can Form Two Arrays of Equal XOR | [Link](https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/) | [Link](./lib/medium/1442_count_triplets_that_can_form_two_arrays_of_equal_xor.rb) | [Link](./test/medium/test_1442_count_triplets_that_can_form_two_arrays_of_equal_xor.rb) |
697697
| 1448. Count Good Nodes in Binary Tree | [Link](https://leetcode.com/problems/count-good-nodes-in-binary-tree/) | [Link](./lib/medium/1448_count_good_nodes_in_binary_tree.rb) | [Link](./test/medium/test_1448_count_good_nodes_in_binary_tree.rb) |
698+
| 1457. Pseudo-Palindromic Paths in a Binary Tree | [Link](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/) | [Link](./lib/medium/1457_pseudo_palindromic_paths_in_a_binary_tree.rb) | [Link](./test/medium/test_1457_pseudo_palindromic_paths_in_a_binary_tree.rb) |
698699
| 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) |
699700
| 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) |
700701
| 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

+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.6.1'
8+
s.version = '8.6.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,23 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/
4+
# @param {TreeNode} root
5+
# @return {Integer}
6+
def pseudo_palindromic_paths(root)
7+
find_for_pseudo_palindromic_path(root, 0)
8+
end
9+
10+
private
11+
12+
# @param {TreeNode} root
13+
# @param {Integer} freq
14+
# @return {Integer}
15+
def find_for_pseudo_palindromic_path(node, freq)
16+
return 0 unless node
17+
18+
freq = freq ^ (1 << node.val)
19+
20+
return (freq & (freq - 1)).zero? ? 1 : 0 if !node.left && !node.right
21+
22+
find_for_pseudo_palindromic_path(node.left, freq) + find_for_pseudo_palindromic_path(node.right, freq)
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/binary_tree'
5+
require_relative '../../lib/medium/1457_pseudo_palindromic_paths_in_a_binary_tree'
6+
require 'minitest/autorun'
7+
8+
class PseudoPalindromicPathsInABinaryTreeTest < ::Minitest::Test
9+
def test_default_one
10+
assert_equal(
11+
2,
12+
pseudo_palindromic_paths(
13+
::TreeNode.build_tree(
14+
[2, 3, 1, 3, 1, nil, 1]
15+
)
16+
)
17+
)
18+
end
19+
20+
def test_default_two
21+
assert_equal(
22+
1,
23+
pseudo_palindromic_paths(
24+
::TreeNode.build_tree(
25+
[2, 1, 1, 1, 3, nil, nil, nil, nil, nil, 1]
26+
)
27+
)
28+
)
29+
end
30+
31+
def test_default_three
32+
assert_equal(
33+
1,
34+
pseudo_palindromic_paths(
35+
::TreeNode.build_tree(
36+
[9]
37+
)
38+
)
39+
)
40+
end
41+
end

0 commit comments

Comments
 (0)