From 79ba1b718b84f2c3e34f66acfb99504d50a5f35d Mon Sep 17 00:00:00 2001 From: fartem Date: Tue, 18 Feb 2025 05:35:34 +0300 Subject: [PATCH] 2025-02-18 v. 8.6.1: added "1448. Count Good Nodes in Binary Tree" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- .../1448_count_good_nodes_in_binary_tree.rb | 20 +++++++++ ...st_1448_count_good_nodes_in_binary_tree.rb | 41 +++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 lib/medium/1448_count_good_nodes_in_binary_tree.rb create mode 100644 test/medium/test_1448_count_good_nodes_in_binary_tree.rb diff --git a/README.md b/README.md index 7077c079..87724abd 100644 --- a/README.md +++ b/README.md @@ -694,6 +694,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | | 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) | +| 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) | | 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 db85fe8b..66ba3b25 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.6.0' + s.version = '8.6.1' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1448_count_good_nodes_in_binary_tree.rb b/lib/medium/1448_count_good_nodes_in_binary_tree.rb new file mode 100644 index 00000000..51e1e093 --- /dev/null +++ b/lib/medium/1448_count_good_nodes_in_binary_tree.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/count-good-nodes-in-binary-tree/ +# @param {TreeNode} root +# @return {Integer} +def good_nodes(root) = dfs_for_good_nodes(root, root.val) + +private + +# @param {TreeNode} node +# @param {Integer} curr_max +# @return {Integer} +def dfs_for_good_nodes(node, curr_max) + return 0 unless node + + left = dfs_for_good_nodes(node.left, [curr_max, node.val].max) + right = dfs_for_good_nodes(node.right, [curr_max, node.val].max) + + (node.val >= curr_max ? 1 : 0) + left + right +end diff --git a/test/medium/test_1448_count_good_nodes_in_binary_tree.rb b/test/medium/test_1448_count_good_nodes_in_binary_tree.rb new file mode 100644 index 00000000..43c5776c --- /dev/null +++ b/test/medium/test_1448_count_good_nodes_in_binary_tree.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/common/binary_tree' +require_relative '../../lib/medium/1448_count_good_nodes_in_binary_tree' +require 'minitest/autorun' + +class CountGoodNodesInBinaryTreeTest < ::Minitest::Test + def test_default_one + assert_equal( + 4, + good_nodes( + ::TreeNode.build_tree( + [3, 1, 4, 3, nil, 1, 5] + ) + ) + ) + end + + def test_default_two + assert_equal( + 3, + good_nodes( + ::TreeNode.build_tree( + [3, 3, nil, 4, 2] + ) + ) + ) + end + + def test_default_three + assert_equal( + 1, + good_nodes( + ::TreeNode.build_tree( + [1] + ) + ) + ) + end +end