Skip to content

Commit f7cdfd7

Browse files
committed
O(n) time and O(logn) space for recursive stack
1 parent 3b94216 commit f7cdfd7

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

116. Populating Next Right Pointers in Each Node/116. Populating Next Right Pointers in Each Node.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,13 @@ def __init__(self, val, left, right, next):
2828
self.next = next
2929
"""
3030
class Solution:
31-
def connect(self, root: 'Node') -> 'Node':
32-
if not root:
33-
return None
34-
if root.left:
35-
root.left.next = root.right
31+
def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
32+
if not root: return None
3633
if root.right:
34+
if root.left:
35+
root.left.next = root.right
3736
if root.next:
3837
root.right.next = root.next.left
39-
else:
40-
root.right.next = None
4138
self.connect(root.left)
4239
self.connect(root.right)
4340
return root

0 commit comments

Comments
 (0)