-
Notifications
You must be signed in to change notification settings - Fork 161
/
find-elements-in-a-contaminated-binary-tree.py
51 lines (48 loc) · 1.26 KB
/
find-elements-in-a-contaminated-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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class FindElements(object):
def __init__(self, root):
"""
:type root: TreeNode
"""
self.root = root
if not root:
return
root.val = 0
self.recover(root)
return
def recover(self, root):
if not root:
return
if root.left:
root.left.val = root.val * 2 + 1
self.recover(root.left)
if root.right:
root.right.val = root.val * 2 + 2
self.recover(root.right)
def find(self, target):
"""
:type target: int
:rtype: bool
"""
node = self.root
encoding = bin(target + 1)[3:]
counter = 0
while counter < len(encoding):
if encoding[counter] == "0":
if node.left:
node = node.left
counter += 1
else:
return False
else:
if node.right:
node = node.right
counter += 1
else:
return False
return True