简繁EN

LeetCodeNo. 085

[LeetCode with Python] 117. Populating Next Right Pointers in Each Node II

Contents

Problem

Problem page: https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
Topics: Tree, Depth-first SearchTwo Pointers
Difficulty: Hard
Similar problems: (M) Populating Next Right Pointers in Each Node

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

Analysis

Unlike in Populating Next Right Pointers in Each Node, the tree here isn't guaranteed to be a perfect binary tree. Nodes can be missing children. This problem also allows only constant extra space.
My code for that problem uses cur.right.next = cur.next.left to link two adjacent children (cur is the current node on the level above). In this problem, two nodes next to each other on the next level can have parents with several childless nodes between them on the level above. In the example, node 3 has no left child, but that code still tries to link its two children with cur.left.next = cur.right. That's where it fails.
The idea is still to link the nodes one level at a time, starting with the first level, where there's just the root and nothing to link. The next pointers on the level above are already set. I follow them from left to right. At each node, I append its children, if it has any, to the end of the linked list for the next level, left child first, then right.
I use dummy as the dummy node at the head of the linked list for the next level. tail points to the current end, so even the first child just goes after tail.
After walking the level above, I set cur to dummy.next. I need to reset dummy.next to None before I start each level. Otherwise, when I reach the bottom level, dummy.next still has its value from the previous pass, so cur never becomes None and the loop over the levels never stops. I don't need to set next on the last node in the linked list. It's NULL to begin with.
The time complexity is O(n). I visit each node once, and each node except the root also gets appended to a linked list once. The only extra space is for the three variables dummy, tail, and cur, so it's O(1).


Code

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
# Definition for a  binary tree node
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
self.next = None

class Solution:
# @param root, a tree node
# @return nothing
def connect(self, root):
if None == root:
return [ ]
dummy = TreeNode(-1)
cur = root
while None != cur:
dummy.next = None
tail = dummy
while None != cur:
if None != cur.left:
tail.next = cur.left
tail = tail.next
if None != cur.right:
tail.next = cur.right
tail = tail.next
cur = cur.next
cur = dummy.next

Article link: https://xnerv.wang/en/leetcode-populating-next-right-pointers-in-each-node-ii/