0
0
DSA Goprogramming~20 mins

Boundary Traversal of Binary Tree in DSA Go - Practice Problems & Challenges

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

import "fmt"

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

func boundaryTraversal(root *Node) []int {
    if root == nil {
        return []int{}
    }
    var res []int
    res = append(res, root.data)

    // Left boundary (excluding leaf)
    curr := root.left
    for curr != nil {
        if curr.left != nil || curr.right != nil {
            res = append(res, curr.data)
        }
        if curr.left != nil {
            curr = curr.left
        } else {
            curr = curr.right
        }
    }

    // Leaves
    var addLeaves func(*Node)
    addLeaves = func(node *Node) {
        if node == nil {
            return
        }
        if node.left == nil && node.right == nil {
            res = append(res, node.data)
            return
        }
        addLeaves(node.left)
        addLeaves(node.right)
    }
    addLeaves(root.left)
    addLeaves(root.right)

    // Right boundary (excluding leaf) in reverse
    var stack []int
    curr = root.right
    for curr != nil {
        if curr.left != nil || curr.right != nil {
            stack = append(stack, curr.data)
        }
        if curr.right != nil {
            curr = curr.right
        } else {
            curr = curr.left
        }
    }
    for i := len(stack) - 1; i >= 0; i-- {
        res = append(res, stack[i])
    }

    return res
}

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

    result := boundaryTraversal(root)
    fmt.Println(result)
}
A[1, 2, 4, 5, 6, 7, 3]
B[1, 2, 4, 5, 7, 6, 3]
C[1, 2, 4, 5, 6, 3, 7]
D[1, 4, 5, 6, 7, 2, 3]
Attempts:
2 left
💡 Hint
Remember to include left boundary (excluding leaves), then leaves, then right boundary in reverse order.
🧠 Conceptual
intermediate
1:30remaining
Understanding Boundary Traversal Components
Which of the following correctly describes the order of nodes visited in a boundary traversal of a binary tree?
ARoot node, all leaf nodes from left to right, left boundary excluding leaves, right boundary excluding leaves in reverse
BAll leaf nodes from left to right, root node, left boundary excluding leaves, right boundary excluding leaves in reverse
CRoot node, left boundary excluding leaves, all leaf nodes from left to right, right boundary excluding leaves in reverse
DLeft boundary excluding leaves, root node, right boundary excluding leaves in reverse, all leaf nodes from left to right
Attempts:
2 left
💡 Hint
Think about the natural boundary around the tree starting from the root.
🔧 Debug
advanced
1:30remaining
Identify the Bug in Boundary Traversal Code
Given the following snippet from a boundary traversal function, what error will it cause if root == nil? func boundaryTraversal(root *Node) []int { var res []int res = append(res, root.data) curr := root.left for curr != nil { if curr.left != nil || curr.right != nil { res = append(res, curr.data) } if curr.left != nil { curr = curr.left } else { curr = curr.right } } // Leaves and right boundary code omitted for brevity return res }
ACompilation error due to missing return statement
BPanic due to nil pointer dereference when accessing root.data if root is nil
CNo error, returns slice with only root data
DInfinite loop because curr never becomes nil
Attempts:
2 left
💡 Hint
Check if root is nil before accessing its fields.
Predict Output
advanced
2:00remaining
Boundary Traversal Output for Skewed Tree
What is the output of the boundary traversal for the following left-skewed binary tree? Tree structure: 1 | 2 | 3 | 4 Code snippet: func main() { root := &Node{1, nil, nil} root.left = &Node{2, nil, nil} root.left.left = &Node{3, nil, nil} root.left.left.left = &Node{4, nil, nil} result := boundaryTraversal(root) fmt.Println(result) }
A[1, 2, 4, 3]
B[1, 4, 3, 2]
C[4, 3, 2, 1]
D[1, 2, 3, 4]
Attempts:
2 left
💡 Hint
In a skewed tree, left boundary and leaves overlap.
🚀 Application
expert
2:30remaining
Number of Nodes in Boundary Traversal Result
Given a perfect binary tree of height 2 (levels 0 to 2), how many nodes will be in the boundary traversal output?
A7
B8
C9
D10
Attempts:
2 left
💡 Hint
Count root (1), left boundary excluding leaves (1), leaves (4), and right boundary excluding leaves (1).