From 94de93780af921d4551f6bfff3ad27e9368c194b Mon Sep 17 00:00:00 2001 From: bt3gl <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Tue, 8 Aug 2023 14:55:51 -0700 Subject: [PATCH] Update bt_count_unival.py --- trees/bt_count_unival.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/trees/bt_count_unival.py b/trees/bt_count_unival.py index 617f13c..f68f846 100644 --- a/trees/bt_count_unival.py +++ b/trees/bt_count_unival.py @@ -2,29 +2,20 @@ # -*- coding: utf-8 -*- # author: bt3gl -''' -Given the root of a binary tree, return the number of uni-value subtrees. -A uni-value subtree means all nodes of the subtree have the same value. -''' + -def count_unival(root: Optional[TreeNode]) -> int: +def count_unival(root) -> int: global count = 0 def dfs(node): - - if not node: + if node is None: return True - - is_uni_left = dfs(node.left) - is_uni_right = dfs(node.right) - - if is_uni_left and is_uni_right: - if node.left and node.left.val != node.val: - return False - if node.right and node.right.val != node.val: - return False + if dfs(node.left) and dfs(node.right): + if (node.left and node.left.val != node.val) or \ + (node.right and node.right.val != node.val): + return False self.count += 1 return True