Skip to content

Commit 0c12922

Browse files
authored
2025-02-18 v. 8.6.1: added "1448. Count Good Nodes in Binary Tree"
2 parents d15587e + 79ba1b7 commit 0c12922

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
694694
| 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) |
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) |
697+
| 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) |
697698
| 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) |
698699
| 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) |
699700
| 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.0'
8+
s.version = '8.6.1'
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,20 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/count-good-nodes-in-binary-tree/
4+
# @param {TreeNode} root
5+
# @return {Integer}
6+
def good_nodes(root) = dfs_for_good_nodes(root, root.val)
7+
8+
private
9+
10+
# @param {TreeNode} node
11+
# @param {Integer} curr_max
12+
# @return {Integer}
13+
def dfs_for_good_nodes(node, curr_max)
14+
return 0 unless node
15+
16+
left = dfs_for_good_nodes(node.left, [curr_max, node.val].max)
17+
right = dfs_for_good_nodes(node.right, [curr_max, node.val].max)
18+
19+
(node.val >= curr_max ? 1 : 0) + left + right
20+
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/1448_count_good_nodes_in_binary_tree'
6+
require 'minitest/autorun'
7+
8+
class CountGoodNodesInBinaryTreeTest < ::Minitest::Test
9+
def test_default_one
10+
assert_equal(
11+
4,
12+
good_nodes(
13+
::TreeNode.build_tree(
14+
[3, 1, 4, 3, nil, 1, 5]
15+
)
16+
)
17+
)
18+
end
19+
20+
def test_default_two
21+
assert_equal(
22+
3,
23+
good_nodes(
24+
::TreeNode.build_tree(
25+
[3, 3, nil, 4, 2]
26+
)
27+
)
28+
)
29+
end
30+
31+
def test_default_three
32+
assert_equal(
33+
1,
34+
good_nodes(
35+
::TreeNode.build_tree(
36+
[1]
37+
)
38+
)
39+
)
40+
end
41+
end

0 commit comments

Comments
 (0)