0
0
DSA Goprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vertical Order Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Vertical Order Traversal for a Simple Tree
What is the output of the vertical order traversal for the given binary tree?
DSA Go
type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func verticalOrder(root *TreeNode) [][]int {
    if root == nil {
        return [][]int{}
    }
    type nodeInfo struct {
        node *TreeNode
        col  int
    }
    queue := []nodeInfo{{root, 0}}
    colTable := map[int][]int{}
    minCol, maxCol := 0, 0
    for len(queue) > 0 {
        curr := queue[0]
        queue = queue[1:]
        colTable[curr.col] = append(colTable[curr.col], curr.node.Val)
        if curr.node.Left != nil {
            queue = append(queue, nodeInfo{curr.node.Left, curr.col - 1})
            if curr.col-1 < minCol {
                minCol = curr.col - 1
            }
        }
        if curr.node.Right != nil {
            queue = append(queue, nodeInfo{curr.node.Right, curr.col + 1})
            if curr.col+1 > maxCol {
                maxCol = curr.col + 1
            }
        }
    }
    result := [][]int{}
    for i := minCol; i <= maxCol; i++ {
        result = append(result, colTable[i])
    }
    return result
}

// Tree structure:
//      1
//     / \
//    2   3
//       / \
//      4   5

root := &TreeNode{1, &TreeNode{2, nil, nil}, &TreeNode{3, &TreeNode{4, nil, nil}, &TreeNode{5, nil, nil}}}
output := verticalOrder(root)
fmt.Println(output)
A[[2], [1, 4], [3], [5]]
B[[1], [2, 3], [4, 5]]
C[[2], [4, 1], [3, 5]]
D[[2, 4], [1, 3], [5]]
Attempts:
2 left
💡 Hint
Think about columns from left to right and nodes in each column by level order.
🧠 Conceptual
intermediate
1:30remaining
Understanding Column Indexing in Vertical Order Traversal
In vertical order traversal, what does the column index represent for each node?
AThe depth level of the node in the tree
BThe horizontal distance from the root node, with left child decreasing and right child increasing the index
CThe order in which nodes are visited during preorder traversal
DThe number of nodes in the subtree rooted at that node
Attempts:
2 left
💡 Hint
Think about how left and right children affect the position horizontally.
🔧 Debug
advanced
2:30remaining
Identify the Bug in Vertical Order Traversal Code
What error will this Go code produce when running vertical order traversal?
DSA Go
func verticalOrder(root *TreeNode) [][]int {
    if root == nil {
        return [][]int{}
    }
    type nodeInfo struct {
        node *TreeNode
        col  int
    }
    queue := []nodeInfo{{root, 0}}
    colTable := map[int][]int{}
    minCol, maxCol := 0, 0
    for len(queue) > 0 {
        curr := queue[0]
        queue = queue[1:]
        colTable[curr.col] = append(colTable[curr.col], curr.node.Val)
        if curr.node.Left != nil {
            queue = append(queue, nodeInfo{curr.node.Left, curr.col - 1}) // Bug here
            if curr.col-1 < minCol {
                minCol = curr.col - 1
            }
        }
        if curr.node.Right != nil {
            queue = append(queue, nodeInfo{curr.node.Right, curr.col + 1}) // Bug here
            if curr.col+1 > maxCol {
                maxCol = curr.col + 1
            }
        }
    }
    result := [][]int{}
    for i := minCol; i <= maxCol; i++ {
        result = append(result, colTable[i])
    }
    return result
}
AOutput will be empty because queue is never updated
BSyntax error due to missing colon in map initialization
CRuntime panic due to nil pointer dereference
DThe traversal will produce reversed columns because left and right child column indices are swapped
Attempts:
2 left
💡 Hint
Check how column indices are assigned for left and right children.
🚀 Application
advanced
1:30remaining
Number of Columns in Vertical Order Traversal
Given the binary tree below, how many columns will the vertical order traversal output contain? // Tree structure: // 10 // / \ // 5 15 // / \ \ // 3 7 18
A3
B4
C5
D6
Attempts:
2 left
💡 Hint
Count columns from leftmost to rightmost node by horizontal distance.
Predict Output
expert
3:00remaining
Output of Vertical Order Traversal with Multiple Nodes in Same Column and Level
What is the output of vertical order traversal for the following tree? // Tree structure: // 1 // / \ // 2 3 // / \ / \ // 4 5 6 7 // \ / // 8 9
DSA Go
type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func verticalOrder(root *TreeNode) [][]int {
    if root == nil {
        return [][]int{}
    }
    type nodeInfo struct {
        node *TreeNode
        col  int
    }
    queue := []nodeInfo{{root, 0}}
    colTable := map[int][]int{}
    minCol, maxCol := 0, 0
    for len(queue) > 0 {
        curr := queue[0]
        queue = queue[1:]
        colTable[curr.col] = append(colTable[curr.col], curr.node.Val)
        if curr.node.Left != nil {
            queue = append(queue, nodeInfo{curr.node.Left, curr.col - 1})
            if curr.col-1 < minCol {
                minCol = curr.col - 1
            }
        }
        if curr.node.Right != nil {
            queue = append(queue, nodeInfo{curr.node.Right, curr.col + 1})
            if curr.col+1 > maxCol {
                maxCol = curr.col + 1
            }
        }
    }
    result := [][]int{}
    for i := minCol; i <= maxCol; i++ {
        result = append(result, colTable[i])
    }
    return result
}

root := &TreeNode{1,
    &TreeNode{2,
        &TreeNode{4, nil, nil},
        &TreeNode{5, nil, &TreeNode{8, nil, nil}},
    },
    &TreeNode{3,
        &TreeNode{6, nil, nil},
        &TreeNode{7, &TreeNode{9, nil, nil}, nil},
    },
}
output := verticalOrder(root)
fmt.Println(output)
A[[4], [2], [1, 5, 6], [3, 8, 9], [7]]
B[[4], [2, 5], [1, 6, 8], [3, 7, 9]]
C[[4], [2, 5, 8], [1, 6], [3, 7, 9]]
D[[4], [2], [1, 5, 6], [3, 7, 8, 9]]
Attempts:
2 left
💡 Hint
Remember nodes in the same column are listed by level order (top to bottom, left to right).