↓Skip to main content
  1. LeetCode/

LeetCode 116: Populating Next Right Pointers in Each Node

·2 mins· ·
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-11-18

  • Submissions: Runtime Error → Wrong Answer ×2 → Accepted → Wrong Answer → Runtime Error → Accepted ×2

Approach
#

Every non-leaf in a perfect binary tree has two children, so the next level has only two kinds of links (Linked List, Others; Tree Template 2):

  • Children of the same parent: node.left.next = node.right.
  • Across neighboring parents: node.right.next = node.next.left.

With the level above already linked through next, walk across it without a queue, starting from the leftmost node on each level.

My Attempts
#

The early versions were recursive but did not return root, so a single node [8] returned None; one did not handle an empty tree. My accepted version was recursive (O(log n) depth); here it is the iterative walk along next.

Solution
#

class Solution:
    def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
        leftmost = root
        while leftmost and leftmost.left:
            node = leftmost
            while node:                              # 沿著上一層的 next 走
                node.left.next = node.right
                if node.next:
                    node.right.next = node.next.left
                node = node.next
            leftmost = leftmost.left
        return root

Complexity
#

Time O(n), extra space O(1).

Mistakes to Avoid
#

  • The function must return root.
  • This relies on a perfect tree; an imperfect one (LC 117) must handle missing children.

Interview Explanation
#

In a perfect tree the next level has only two link types: siblings under one parent, and a right child to the neighboring parent’s left child. I walk each level along the next pointers set on the level above, with no queue. Time O(n), extra space O(1).

Related

LeetCode 1448: Count Good Nodes in Binary Tree
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 1448 note: pass the path maximum down during DFS; a node is good when its value is at least that maximum
LeetCode 199: Binary Tree Right Side View
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 199 note: in a level-order traversal, each level’s rightmost node is what you see from the right
LeetCode 113: Path Sum II
·2 mins
LeetCode Medium Backtracking Tree Depth-First-Search Binary-Tree
LeetCode 113 note: backtrack with the current path and store a copy at a leaf whose total matches; values can be negative, so no early pruning
LeetCode 129: Sum Root to Leaf Numbers
·1 min
LeetCode Medium Tree Depth-First-Search Binary-Tree
LeetCode 129 note: on the way down multiply the current number by 10 and add the node’s value, adding it to the total at a leaf
LeetCode 236: Lowest Common Ancestor of a Binary Tree
·2 mins
LeetCode Medium Tree Depth-First-Search Binary-Tree Binary-Lifting
LeetCode 236 note: recursion returns whichever of p or q a subtree contains; finding something on both sides means they split here, so this node is the LCA
LeetCode 337: House Robber III
·2 mins
LeetCode Medium Dynamic-Programming Tree Depth-First-Search Binary-Tree
LeetCode 337 note: tree DP where each node returns the best (rob it, skip it) pair; not robbing alternate levels