master-algorithms-py/trees/bt_count_unival.py
2023-08-08 14:55:51 -07:00

26 lines
592 B
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def count_unival(root) -> int:
global count = 0
def dfs(node):
if node is None:
return True
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
return False
dfs(root)
return count