0
0
DSA Goprogramming~10 mins

Boundary Traversal of Binary Tree in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new binary tree node with given value.

DSA Go
func newNode(val int) *Node {
    return &Node{
        data: [1],
        left: nil,
        right: nil,
    }
}
Drag options to blanks, or click blank then click option'
Avalue
Bval
Cnode
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the function like 'value' or 'node'.
2fill in blank
medium

Complete the code to check if a node is a leaf node (no children).

DSA Go
func isLeaf(node *Node) bool {
    return node != nil && node.left == nil && node.[1] == nil
}
Drag options to blanks, or click blank then click option'
Aparent
Bchild
Cright
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Checking a non-existent field like 'child' or 'parent'.
3fill in blank
hard

Fix the error in the code to traverse the left boundary of the tree (excluding leaves).

DSA Go
func addLeftBoundary(root *Node, res *[]int) {
    curr := root.left
    for curr != nil {
        if !isLeaf(curr) {
            *res = append(*res, curr.data)
        }
        if curr.left != nil {
            curr = curr.[1]
        } else {
            curr = curr.right
        }
    }
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using curr.right instead of curr.left when left child exists.
4fill in blank
hard

Fill both blanks to add the right boundary nodes in reverse order (excluding leaves).

DSA Go
func addRightBoundary(root *Node, res *[]int) {
    curr := root.[1]
    var tmp []int
    for curr != nil {
        if !isLeaf(curr) {
            tmp = append(tmp, curr.data)
        }
        if curr.[2] != nil {
            curr = curr.[2]
        } else {
            curr = curr.left
        }
    }
    for i := len(tmp) - 1; i >= 0; i-- {
        *res = append(*res, tmp[i])
    }
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from root.left or moving down left children for right boundary.
5fill in blank
hard

Fill all three blanks to collect all leaf nodes in left-to-right order.

DSA Go
func addLeaves(root *Node, res *[]int) {
    if root == nil {
        return
    }
    if isLeaf(root) {
        *res = append(*res, root.[1])
        return
    }
    addLeaves(root.[2], res)
    addLeaves(root.[3], res)
}
Drag options to blanks, or click blank then click option'
Adata
Bleft
Cright
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Appending wrong field like 'value' or traversing right child before left.