-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtree.py
More file actions
55 lines (42 loc) · 1.05 KB
/
tree.py
File metadata and controls
55 lines (42 loc) · 1.05 KB
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
# Pending...
# Tree Game
"""
# Tree Structure
0
/ | \
0 0 0
/| /|\ | \
1 0 4-4 0 -3 0
/|
8 0
# Rule:
* Player 1 starts the game
* Player 2 plays alternate chances
* AIM: To maximize the score of player 1 when he ends the game
* Only the leaf nodes contain the score
# Data Inferred:
* Player 1 plays on even level ids of the tree
* Player 2 plays on odd levels
* Player 2 tries to minimize the score as much as possible and player 1 tries to maximize
# TIP:
* Boil the score up the tree from the leaf nodes to get the scores of each node
* This is not a BST --- Analyze the tree
"""
class Node:
def __init__(self, value):
self.value = value
self.children = []
def game(root, score, level):
if not root.value:
result = []
for child in root.children:
result.append(game(child))
if level%2 == 0:
score = max(result)
else:
score = min(result)
else:
return root.value
score = 0
level = 0
game(root, score, level)