0
0
DSA Goprogramming~20 mins

Top View of Binary Tree in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Top View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Top View for a Simple Binary Tree
What is the output of the top view traversal for the given binary tree?
DSA Go
package main

import "fmt"

type Node struct {
    data  int
    left  *Node
    right *Node
}

func topView(root *Node) []int {
    if root == nil {
        return []int{}
    }
    type pair struct {
        node *Node
        hd   int
    }
    queue := []pair{{root, 0}}
    hdMap := map[int]int{}
    minHd, maxHd := 0, 0
    result := []int{}

    for len(queue) > 0 {
        p := queue[0]
        queue = queue[1:]
        node, hd := p.node, p.hd

        if _, ok := hdMap[hd]; !ok {
            hdMap[hd] = node.data
            if hd < minHd {
                minHd = hd
            }
            if hd > maxHd {
                maxHd = hd
            }
        }

        if node.left != nil {
            queue = append(queue, pair{node.left, hd - 1})
        }
        if node.right != nil {
            queue = append(queue, pair{node.right, hd + 1})
        }
    }

    for i := minHd; i <= maxHd; i++ {
        result = append(result, hdMap[i])
    }
    return result
}

func main() {
    root := &Node{data: 1}
    root.left = &Node{data: 2}
    root.right = &Node{data: 3}
    root.left.right = &Node{data: 4}
    root.left.right.right = &Node{data: 5}
    root.left.right.right.right = &Node{data: 6}

    fmt.Println(topView(root))
}
A[2, 1, 3, 6]
B[1, 2, 3]
C[2, 1, 3, 4, 5, 6]
D[1, 2, 4, 5, 6]
Attempts:
2 left
💡 Hint
Think about horizontal distances and which nodes appear first at each horizontal distance.
🧠 Conceptual
intermediate
1:30remaining
Understanding Horizontal Distance in Top View
In the context of the top view of a binary tree, what does the 'horizontal distance' (hd) represent?
AThe vertical level of a node from the root
BThe distance of a node from the root measured horizontally, with root at 0, left child at hd-1, right child at hd+1
CThe depth of a node in the tree
DThe number of nodes between the root and the current node
Attempts:
2 left
💡 Hint
Horizontal distance helps to identify nodes aligned vertically when viewed from the top.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Top View Implementation
What error will occur when running this code snippet for top view of a binary tree?
DSA Go
func topView(root *Node) []int {
    if root == nil {
        return []int{}
    }
    type pair struct {
        node *Node
        hd   int
    }
    queue := []pair{{root, 0}}
    hdMap := map[int]int{}
    minHd, maxHd := 0, 0
    result := []int{}

    for len(queue) > 0 {
        p := queue[0]
        queue = queue[1:]
        node, hd := p.node, p.hd

        hdMap[hd] = node.data
        if hd < minHd {
            minHd = hd
        }
        if hd > maxHd {
            maxHd = hd
        }

        if node.left != nil {
            queue = append(queue, pair{node.left, hd - 1})
        }
        if node.right != nil {
            queue = append(queue, pair{node.right, hd + 1})
        }
    }

    for i := minHd; i <= maxHd; i++ {
        result = append(result, hdMap[i])
    }
    return result
}
AInfinite loop because queue is never emptied
BSyntax error due to missing colon in map declaration
CThe map overwrites values, so the top view is incorrect
DRuntime panic due to nil pointer dereference
Attempts:
2 left
💡 Hint
Check how the map is updated for each horizontal distance.
🚀 Application
advanced
2:30remaining
Top View of a Complex Binary Tree
Given the binary tree below, what is the top view output? Tree structure: 10 / \ 20 30 / \ \ 40 60 50 / \ 70 80
A[40, 20, 60, 10, 30, 50, 80]
B[40, 20, 10, 30, 50]
C[20, 10, 30, 50, 80]
D[40, 20, 10, 30, 50, 80]
Attempts:
2 left
💡 Hint
Consider horizontal distances and which nodes appear first at each horizontal distance.
Predict Output
expert
3:00remaining
Output of Top View with Multiple Nodes at Same Horizontal Distance
What is the output of the top view traversal for the following binary tree? Tree: 1 / \ 2 3 / \ \ 4 5 6 \ \ 7 8
DSA Go
package main

import "fmt"

type Node struct {
    data  int
    left  *Node
    right *Node
}

func topView(root *Node) []int {
    if root == nil {
        return []int{}
    }
    type pair struct {
        node *Node
        hd   int
    }
    queue := []pair{{root, 0}}
    hdMap := map[int]int{}
    minHd, maxHd := 0, 0
    result := []int{}

    for len(queue) > 0 {
        p := queue[0]
        queue = queue[1:]
        node, hd := p.node, p.hd

        if _, ok := hdMap[hd]; !ok {
            hdMap[hd] = node.data
            if hd < minHd {
                minHd = hd
            }
            if hd > maxHd {
                maxHd = hd
            }
        }

        if node.left != nil {
            queue = append(queue, pair{node.left, hd - 1})
        }
        if node.right != nil {
            queue = append(queue, pair{node.right, hd + 1})
        }
    }

    for i := minHd; i <= maxHd; i++ {
        result = append(result, hdMap[i])
    }
    return result
}

func main() {
    root := &Node{data: 1}
    root.left = &Node{data: 2}
    root.right = &Node{data: 3}
    root.left.left = &Node{data: 4}
    root.left.right = &Node{data: 5}
    root.right.right = &Node{data: 6}
    root.left.right.right = &Node{data: 7}
    root.right.right.right = &Node{data: 8}

    fmt.Println(topView(root))
}
A[4, 2, 1, 3, 6, 8]
B[4, 2, 1, 5, 6, 8]
C[2, 1, 3, 6, 8]
D[4, 2, 1, 3, 7, 8]
Attempts:
2 left
💡 Hint
Remember only the first node at each horizontal distance is included in the top view.