-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunivalued_binary_tree.py
59 lines (44 loc) · 1.46 KB
/
univalued_binary_tree.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
# 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 isUnivalTree(root):
arr = []
def traversal(root):
if root == None: return
#print("inside if")
traversal(root.left)
traversal(root.right)
arr.append(root.val)
#print (root.left.val)
traversal(root)
#print (arr)
#print (len(set(arr)))
if len(set(arr)) >=2:
return False
else:
return True
#go to leet code to test the code with a proper treeNode input! thanks
#second method
'''
# 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 isUnivalTree(self, root: Optional[TreeNode]) -> bool:
if root == None:
return True
if(root.left != None):
if(root.left.val != root.val):
return False
if(root.right != None):
if(root.right.val != root.val):
return False
return self.isUnivalTree(root.left) and self.isUnivalTree(root.right)
'''