0
0
DSA Goprogramming~20 mins

Maximum Width of Binary Tree in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Maximum Width Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Maximum Width Calculation for a Simple Tree
What is the output of the following Go code that calculates the maximum width of a binary tree?
DSA Go
package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func widthOfBinaryTree(root *TreeNode) int {
    if root == nil {
        return 0
    }
    maxWidth := 0
    queue := []struct {
        node  *TreeNode
        index int
    }{{root, 0}}

    for len(queue) > 0 {
        levelLength := len(queue)
        startIndex := queue[0].index
        endIndex := queue[levelLength-1].index
        if endIndex-startIndex+1 > maxWidth {
            maxWidth = endIndex - startIndex + 1
        }
        newQueue := []struct {
            node  *TreeNode
            index int
        }{}
        for i := 0; i < levelLength; i++ {
            current := queue[i]
            if current.node.Left != nil {
                newQueue = append(newQueue, struct {
                    node  *TreeNode
                    index int
                }{current.node.Left, 2*(current.index - startIndex) + 1})
            }
            if current.node.Right != nil {
                newQueue = append(newQueue, struct {
                    node  *TreeNode
                    index int
                }{current.node.Right, 2*(current.index - startIndex) + 2})
            }
        }
        queue = newQueue
    }
    return maxWidth
}

func main() {
    root := &TreeNode{Val: 1}
    root.Left = &TreeNode{Val: 2}
    root.Right = &TreeNode{Val: 3}
    root.Left.Left = &TreeNode{Val: 4}
    root.Right.Right = &TreeNode{Val: 5}
    fmt.Println(widthOfBinaryTree(root))
}
A5
B4
C3
D2
Attempts:
2 left
💡 Hint
Consider how the width is calculated including null positions between nodes.
🧠 Conceptual
intermediate
1:30remaining
Understanding Indexing in Maximum Width Calculation
In the maximum width calculation of a binary tree, why do we subtract the start index from each node's index before calculating the child indices?
ATo ensure the indices start from zero at each level for accurate width calculation
BTo skip null nodes in the tree and only count existing nodes
CTo prevent integer overflow by normalizing indices at each level
DTo double the indices for left and right children
Attempts:
2 left
💡 Hint
Think about how large index values can grow in a deep tree.
🔧 Debug
advanced
2:00remaining
Identify the Error in Width Calculation Code
What error will the following Go code produce when calculating the maximum width of a binary tree?
DSA Go
func widthOfBinaryTree(root *TreeNode) int {
    if root == nil {
        return 0
    }
    maxWidth := 0
    queue := []struct {
        node  *TreeNode
        index int
    }{{root, 1}}

    for len(queue) > 0 {
        levelLength := len(queue)
        startIndex := queue[0].index
        endIndex := queue[levelLength-1].index
        if endIndex-startIndex+1 > maxWidth {
            maxWidth = endIndex - startIndex + 1
        }
        newQueue := []struct {
            node  *TreeNode
            index int
        }{}
        for i := 0; i < levelLength; i++ {
            current := queue[i]
            if current.node.Left != nil {
                newQueue = append(newQueue, struct {
                    node  *TreeNode
                    index int
                }{current.node.Left, 2*current.index})
            }
            if current.node.Right != nil {
                newQueue = append(newQueue, struct {
                    node  *TreeNode
                    index int
                }{current.node.Right, 2*current.index + 1})
            }
        }
        queue = newQueue
    }
    return maxWidth
}
AThe width calculation will be incorrect because indices are not normalized per level
BThe code will panic due to integer overflow on large trees
CThe code will not compile due to missing import statements
DThe code will return zero for all trees
Attempts:
2 left
💡 Hint
Check how indices are assigned to child nodes and how they relate to the start index of the level.
🚀 Application
advanced
2:30remaining
Maximum Width of a Sparse Binary Tree
Given a binary tree where some nodes are missing, what is the maximum width of the tree after running the width calculation algorithm?
DSA Go
root := &TreeNode{Val: 1}
root.Left = &TreeNode{Val: 2}
root.Right = &TreeNode{Val: 3}
root.Left.Right = &TreeNode{Val: 4}
root.Right.Right = &TreeNode{Val: 5}
root.Left.Right.Left = &TreeNode{Val: 6}
A4
B3
C6
D5
Attempts:
2 left
💡 Hint
Consider the positions of nodes including gaps caused by missing nodes.
🧠 Conceptual
expert
1:30remaining
Why Use BFS with Indexing for Maximum Width?
Why is breadth-first search (BFS) combined with indexing nodes important for calculating the maximum width of a binary tree?
ABFS visits nodes level by level and indexing tracks positions to measure width including gaps
BBFS is faster than depth-first search (DFS) for all tree operations
CIndexing nodes allows skipping null nodes to reduce memory usage
DBFS with indexing avoids recursion and stack overflow errors
Attempts:
2 left
💡 Hint
Think about how width is defined and how BFS helps measure it.