-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path1261.py
72 lines (63 loc) · 2.26 KB
/
1261.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
__________________________________________________________________________________________________
sample 64 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class FindElements:
def __init__(self, root: TreeNode):
self.root = root
self.d = {}
if self.root is not None:
self.root.val = 0
self.fillin(self.root)
self.d[0] = True
def fillin(self, root):
if root is None or (root.left is None and root.right is None):
return
if root.left is not None and root.right is not None:
root.left.val = root.val * 2 + 1
root.right.val = root.val * 2 + 2
self.d[root.left.val] = True
self.d[root.right.val] = True
self.fillin(root.left)
self.fillin(root.right)
elif root.left is not None:
root.left.val = root.val * 2 + 1
self.d[root.left.val] = True
self.fillin(root.left)
else:
root.right.val = root.val * 2 + 2
self.d[root.right.val] = True
self.fillin(root.right)
def find(self, target: int) -> bool:
if not self.d:
return False
return target in self.d
# Your FindElements object will be instantiated and called as such:
# obj = FindElements(root)
# param_1 = obj.find(target)
__________________________________________________________________________________________________
sample 68 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class FindElements:
def recoverTree(self, root, val):
if not root:
return
root.val = val
self.values.add(val)
if root.left:
self.recoverTree(root.left, 2 * val + 1)
if root.right:
self.recoverTree(root.right, 2 * val + 2)
def __init__(self, root: TreeNode):
self.values = set()
self.recoverTree(root, 0)
__________________________________________________________________________________________________