-
Notifications
You must be signed in to change notification settings - Fork 43
/
count-univalue-subtrees.py
87 lines (78 loc) · 2.6 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# V0
# V1
# https://xingxingpark.com/Leetcode-250-Count-Univalue-Subtrees/
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def countUnivalSubtrees(self, root):
self.count = 0
self.checkUni(root)
return self.count
# If both children are "True" and root.val is equal to both children's values that exist,
# then root node is uniValue subtree node.
def checkUni(self, root):
if not root:
return True
l, r = self.checkUni(root.left), self.checkUni(root.right)
if l and r and (not root.left or root.left.val == root.val) and \
(not root.right or root.right.val == root.val):
self.count += 1
return True
return False
# V1'
# https://www.jiuzhang.com/solution/count-univalue-subtrees/
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: the given tree
@return: the number of uni-value subtrees.
"""
def countUnivalSubtrees(self, root):
# write your code here
self.count = 0
self.checkUni(root)
return self.count
# bottom-up, first check the leaf nodes and count them,
# then go up, if both children are "True" and root.val is
# equal to both children's values if exist, then root node
# is uniValue suntree node.
def checkUni(self, root):
if not root:
return True
l, r = self.checkUni(root.left), self.checkUni(root.right)
if l and r and (not root.left or root.left.val == root.val) and \
(not root.right or root.right.val == root.val):
self.count += 1
return True
return False
# V2
# Time: O(n)
# Space: O(h)
class Solution(object):
# @param {TreeNode} root
# @return {integer}
def countUnivalSubtrees(self, root):
[is_uni, count] = self.isUnivalSubtrees(root, 0)
return count
def isUnivalSubtrees(self, root, count):
if not root:
return [True, count]
[left, count] = self.isUnivalSubtrees(root.left, count)
[right, count] = self.isUnivalSubtrees(root.right, count)
if self.isSame(root, root.left, left) and \
self.isSame(root, root.right, right):
count += 1
return [True, count]
return [False, count]
def isSame(self, root, child, is_uni):
return not child or (is_uni and root.val == child.val)