Programming & Development / April 8, 2025

LeetCode 117: Populating Next Right Pointers in Each Node II in Go – Solution Using Level Order Traversal

Go Golang LeetCode Populating Next Right Pointers Next Right Pointers Binary Tree BFS Tree Traversal Level Order Linked List

Introduction

LeetCode 117: Populating Next Right Pointers in Each Node II extends the previous problem by asking us to populate each node’s next pointer in a perfect binary tree. The key difference here is that the tree may not be perfect (i.e., it can have missing children), and the task is still to connect each node's next pointer to the node on its right at the same level. If no node exists, the next pointer should be set to null.

This is an important problem because it builds on level-order traversal concepts and requires efficient tree traversal and connection of nodes.

Problem Statement

Given a binary tree, populate each node's next pointer to point to its next right node. If there is no next right node, the next pointer should be set to null.

Unlike the previous problem, the binary tree in this case might not be perfect, and some nodes may not have left or right children.

Example:

go

Input: root = [1,2,3,4,5,null,7]
Output: [1,null,2,3,null,4,5,null,7,null]

Approach:

Level-Order Traversal Using BFS

To solve this problem, we can use a level-order traversal (similar to BFS) with the following steps:

  1. Traverse the Tree Level by Level:
  • Use a queue to handle each level and connect nodes on the same level. For each node, connect its left child to its right child if both exist, and connect its right child to the next node's left child (if there’s a next node).
  1. Queue to Handle Nodes at Each Level:
  • As we traverse each level, we use a queue to store nodes at that level. We process each node in the queue, setting their next pointer to the next node in the queue.
  1. Using null to End a Level:
  • We use nil to mark the end of a level. This will help us understand when we have finished processing one level and when we need to move to the next level.

Go Implementation

Solution Using BFS (Level-Order Traversal)

go

package main

// TreeNode represents a node in a binary tree.
type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
    Next  *TreeNode
}

// connect populates the next right pointers of each node.
func connect(root *TreeNode) *TreeNode {
    if root == nil {
        return nil
    }

    // Initialize the current level pointer
    currentLevelHead := root

    // Traverse the tree level by level
    for currentLevelHead != nil {
        // Dummy node to mark the beginning of the next level
        dummy := &TreeNode{}
        prev := dummy

        // Iterate through nodes at the current level
        current := currentLevelHead
        for current != nil {
            if current.Left != nil {
                prev.Next = current.Left // Connect the left child
                prev = prev.Next
            }
            if current.Right != nil {
                prev.Next = current.Right // Connect the right child
                prev = prev.Next
            }
            current = current.Next // Move to the next node in the current level
        }

        // Move to the next level
        currentLevelHead = dummy.Next
    }

    return root
}

Explanation:

  1. TreeNode Structure:
  • The TreeNode struct represents a node in the binary tree with an integer value (Val), pointers to the left and right children (Left, Right), and a pointer to the next node on the same level (Next).
  1. connect Function:
  • The connect function performs a level-order traversal to connect the next pointers of nodes at the same level.
  • The outer loop (for currentLevelHead != nil) iterates through the tree level by level, and the inner loop processes nodes in the current level.
  • A dummy node is used to mark the beginning of the next level. This helps simplify the logic of connecting nodes at the next level.
  • At each level, we connect the left and right children of each node to the next pointer of the node. After finishing a level, we move to the next level using the dummy.Next pointer.

Time and Space Complexity

MetricComplexityTime ComplexityO(n)Space ComplexityO(1)

Where:

  • n is the number of nodes in the tree.

Time Complexity:

  • The time complexity is O(n) because we traverse all n nodes in the tree exactly once.

Space Complexity:

  • The space complexity is O(1) because we are using constant space, excluding the space required for the input tree itself. We only use a few additional pointers like prev and dummy.

Edge Cases

  1. Empty Tree:
  • Input: root = nil
  • Output: nil
  • Explanation: An empty tree should return nil.
  1. Single Node Tree:
  • Input: root = [1]
  • Output: [1, null]
  • Explanation: A tree with only one node has no next node, so the next pointer is set to null.
  1. Sparse Tree (Some Nodes Missing Children):
  • Input: root = [1,2,3,4,5,null,7]
  • Output: [1,null,2,3,null,4,5,null,7,null]
  • Explanation: Nodes that have no children should have null in their next pointers.

Conclusion

LeetCode 117: Populating Next Right Pointers in Each Node II is a problem that builds upon level-order traversal and requires connecting nodes in a binary tree using next pointers. In this solution, we used a level-order traversal (BFS) with a dummy node to easily connect the next pointers for each node in the tree.

The algorithm runs in O(n) time and uses O(1) extra space, which is an efficient solution for this problem.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex