-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path965.py
54 lines (46 loc) · 1.57 KB
/
965.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
__________________________________________________________________________________________________
sample 20 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
from collections import deque
class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
if not root:
return False
value = root.val
q = deque([root])
while len(q):
node = q.popleft()
if not node:
continue
if node.val != value:
return False
q.append(node.left)
q.append(node.right)
return True
__________________________________________________________________________________________________
sample 12988 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
self.value = root.val
self.isunival = True
def checkvalue(node: TreeNode):
if node:
if node.val != self.value:
self.isunival = False
else:
checkvalue(node.left)
checkvalue(node.right)
checkvalue(root)
return self.isunival
__________________________________________________________________________________________________