-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpopulating-next-right-pointers-in-each-node-ii.py
More file actions
66 lines (61 loc) · 2.36 KB
/
populating-next-right-pointers-in-each-node-ii.py
File metadata and controls
66 lines (61 loc) · 2.36 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
56
57
58
59
60
61
62
63
64
65
66
# https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
#
# Follow up for problem "Populating Next Right Pointers in Each Node".
#
# What if the given tree could be any binary tree? Would your previous solution still work?
#
# Note:
#
# You may only use constant extra space.
# For example,
# Given the following binary tree,
# 1
# / \
# 2 3
# / \ \
# 4 5 7
# After calling your function, the tree should look like:
# 1 -> NULL
# / \
# 2 -> 3 -> NULL
# / \ \
# 4-> 5 -> 7 -> NULL
# My Previous solution would perfectly work for this quesiton as well with no modification at all
class Solution(object):
def connect(self, root):
"""
:type root: TreeLinkNode
:rtype: nothing
"""
# Basic idea : Do a level order traversal and have a list of list of objects
# Do another parse and change the third pointer
final_results = []
self.root = root
if self.root is None:
return None
elif self.root.left is None and self.root.right is None :
pass
else:
# do a level order traversal ( this is exactly the same as the previous level order traversal we've done)
# except that , instead of values - we are appending nodes per level
processing_queue = []
processing_queue.append(self.root)
while processing_queue:
level_vals,length = [],len(processing_queue)
for i in range(length):
temp_node = processing_queue[0]
level_vals.append(temp_node)
if temp_node.left:
processing_queue.append(temp_node.left)
if temp_node.right:
processing_queue.append(temp_node.right)
processing_queue.pop(0)
final_results.append(level_vals)
# go through the final_results again and link them sideways
for i in xrange(len(final_results)):
if len(final_results[i])==1:
continue
else:
for j in xrange(len(final_results[i])-1):
final_results[i][j].next = final_results[i][j+1]
# Cant have tests for this solution until I figure out how to serialize the tree