117. Populating Next Right Pointers in Each Node II
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 55% Topics: Linked List, Tree, Depth-First Search, Breadth-First Search, Binary Tree
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
# Definition for a Node.
class Node(object):
def __init__(self, val=0, left=None, right=None, next=None):
self.val = val
self.left = left
self.right = right
self.next = next
class Solution(object):
# @param root, a tree node
# @return nothing
def connect(self, root):
head = root
pre = Node(0)
cur = pre
while root:
while root:
if root.left:
cur.next = root.left
cur = cur.next
if root.right:
cur.next = root.right
cur = cur.next
root = root.next
root, cur = pre.next, pre
cur.next = None
return head
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions