-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink_right_levelorder.py
74 lines (68 loc) · 1.74 KB
/
link_right_levelorder.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
73
74
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
self.nextRight = None
def height(root):
if root is None:
return 0
else :
lheight = height(root.left)
rheight = height(root.right)
if lheight > rheight:
return lheight+1
else:
return rheight+1
def samelevel(root, i, nodes):
if root is None:
return []
elif i==1:
#return [root.val]
return[root]
else:
return samelevel(root.left, i-1, nodes)+samelevel(root.right, i-1, nodes)
def levelorder(root):
h = height(root)
for i in range(1,h+1):
nodes = []
nodes = samelevel(root,i, nodes)
#print nodes
for j in range(len(nodes)-1):
#print nodes[j].val
nodes[j].nextRight = nodes[j+1]
#print nodes[j].nextRight.val
#print nodes[len(nodes)-1].val
nodes[len(nodes)-1].nextRight = None
#print nodes[len(nodes)-1].nextRight
'''def nextright(root):
h = height(root)
for i in range(1,h+1):
nodes = []
nodes = samelevel(root,i, nodes)
#print nodes
for j in range(len(nodes)-1):
if nodes[j].nextRight is not None :
print nodes[j].nextRight.val
else:
print "None"
'''
node1 = Node(3)
node2 = Node(1)
node3 = Node(7)
node4 = Node(2)
node5 = Node(4)
node7 = Node(9)
node1.left = node2
node1.right = node3
node2.right = node4
node3.left = node5
node3.right = node7
levelorder(node1)
#nextright(node1)
print (node1.nextRight)
print (node2.nextRight.val)
print (node3.nextRight)
print (node4.nextRight.val)
print (node5.nextRight.val)
print node7.nextRight