-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_116.py
51 lines (39 loc) · 1022 Bytes
/
problem_116.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
from random import random, randint
class Node:
def __init__(self, val):
self.val = val
self._left = None
self._right = None
def __repr__(self):
string = "{"
string += "{}: ".format(self.val)
string += "{"
string += "l: {}, ".format(self._left if self._left else -1)
string += "r: {}".format(self._right if self._right else -1)
string += "}"
string += "}"
return string
@staticmethod
def generate():
return Node(randint(1, 100000))
@property
def left(self):
if not self._left:
self._left = Node.generate()
return self._left
@property
def right(self):
if not self._right:
self._right = Node.generate()
return self._right
tree_size = 100
a = Node.generate()
ref = a
nodes = set()
i = 0
while i < tree_size:
ref = ref.left if random() < 0.5 else ref.right
nodes.add(ref)
i += 1
assert len(nodes) == tree_size
print(a)