-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count Univalue Subtrees.py
36 lines (35 loc) · 1.26 KB
/
Count Univalue Subtrees.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def countUnivalSubtrees(self, root: Optional[TreeNode]) -> int:
global ans
ans=0
def dfs(node):
if(node is None):
return True
isLeftUni=dfs(node.left)
isRightUni=dfs(node.right)
left_val=node.left.val if node.left else None
right_val=node.right.val if node.right else None
if(isLeftUni and isRightUni):
global ans
uni_status=False #univalue
if(left_val==right_val==None):
#leaf node
uni_status=True
elif(left_val is None and right_val==node.val):
uni_status=True
elif(right_val is None and left_val==node.val):
uni_status=True
elif(node.val==left_val==right_val):
uni_status=True
if(uni_status):
ans+=1 #increment count
return True
return False
dfs(root)
return ans