-
Notifications
You must be signed in to change notification settings - Fork 0
/
0117_populating_next_right_pointers_in_each_node_ii.py
57 lines (47 loc) · 1.55 KB
/
0117_populating_next_right_pointers_in_each_node_ii.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
from collections import deque
from typing import List, Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
self.next = None
def print_level_order(self):
next_level_root = self
while next_level_root:
current = next_level_root
next_level_root = None
while current:
print(str(current.val) + " ", end='')
if not next_level_root:
if current.left:
next_level_root = current.left
elif current.right:
next_level_root = current.right
current = current.next
print()
class Solution:
def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
# BFS, TC = O(n), SC = O(n)
deq = deque()
if root:
deq.append([root, 0])
while deq:
node, level = deq.popleft()
if node.left:
deq.append([node.left, level + 1])
if node.right:
deq.append([node.right, level + 1])
if deq and level == deq[0][1]:
node.next = deq[0][0]
return root
if __name__ == '__main__':
root = TreeNode(12)
root.left = TreeNode(7)
root.right = TreeNode(1)
root.left.left = TreeNode(9)
root.right.left = TreeNode(10)
root.right.right = TreeNode(5)
solution = Solution()
root = solution.connect(root)
root.print_level_order()