-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path116.py
More file actions
29 lines (28 loc) · 920 Bytes
/
116.py
File metadata and controls
29 lines (28 loc) · 920 Bytes
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
"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution:
def connect(self, root: 'Node') -> 'Node':
def connect_level(root: 'Node', parent:'Node' = None, is_left: bool = True) -> None:
if not parent:
root.next = None
else:
if is_left:
root.next = parent.right
else:
if parent.next:
root.next = parent.next.left
if root.right:
connect_level(root.right, root, False)
if root.left:
connect_level(root.left, root, True)
if not root:
return None
connect_level(root)
return root