Tree Traversal Inorder Left Root Right in DSA Go - Time & Space Complexity
We want to understand how the time needed to visit all nodes in a tree grows as the tree gets bigger.
Specifically, we ask: How does the time to do an inorder traversal change with the number of nodes?
Analyze the time complexity of the following code snippet.
// Inorder traversal: Left, Root, Right
func inorderTraversal(root *TreeNode) {
if root == nil {
return
}
inorderTraversal(root.Left)
fmt.Println(root.Val)
inorderTraversal(root.Right)
}
This code visits every node in a binary tree in the order: left child, node itself, then right child.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Exactly once per node in the tree.
Each node is visited once, so the total steps grow directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The work grows in a straight line with the number of nodes.
Time Complexity: O(n)
This means the time to complete the traversal grows directly in proportion to the number of nodes.
[X] Wrong: "The recursion makes this traversal take more than linear time because it calls itself many times."
[OK] Correct: Each node is visited only once, so the total work is still proportional to the number of nodes, not more.
Understanding this traversal's time helps you explain how tree operations scale, a key skill for many coding problems.
"What if we changed inorder traversal to preorder traversal? How would the time complexity change?"
Practice
Solution
Step 1: Understand inorder traversal definition
Inorder traversal means visiting the left subtree first, then the root node, and finally the right subtree.Step 2: Apply the traversal order
Following this order ensures nodes are visited in ascending order for binary search trees.Final Answer:
Left subtree, Root node, Right subtree -> Option DQuick Check:
Inorder = Left, Root, Right [OK]
- Confusing inorder with preorder or postorder
- Visiting root before left subtree
- Visiting right subtree before root
type Node struct { Val int; Left, Right *Node }?Solution
Step 1: Understand pointer usage in Go for trees
Binary trees use pointers to nodes to allow recursive traversal and modification.Step 2: Choose function signature returning slice of ints
Inorder traversal returns a list of node values, so return type should be []int, and input should be pointer to Node.Final Answer:
func inorder(root *Node) []int -> Option AQuick Check:
Pointer receiver and slice return type [OK]
- Passing Node by value instead of pointer
- Returning single int instead of slice
- Using incorrect return type
2
/ \
1 3
Given the Go code:
type Node struct {
Val int
Left, Right *Node
}
func inorder(root *Node) []int {
if root == nil {
return []int{}
}
left := inorder(root.Left)
right := inorder(root.Right)
return append(append(left, root.Val), right...)
}
func main() {
root := &Node{2, &Node{1, nil, nil}, &Node{3, nil, nil}}
result := inorder(root)
fmt.Println(result)
}
Solution
Step 1: Trace inorder traversal on given tree
Visit left subtree (1), then root (2), then right subtree (3).Step 2: Confirm output list order
The traversal returns [1, 2, 3] as expected for inorder.Final Answer:
[1, 2, 3] -> Option CQuick Check:
Inorder traversal output = [1, 2, 3] [OK]
- Appending root before left subtree
- Appending right subtree before root
- Confusing preorder or postorder output
func inorder(root *Node) []int {
if root == nil {
return []int{}
}
left := inorder(root.Left)
right := inorder(root.Right)
return append(left, right...)
return append(result, root.Val)
}Solution
Step 1: Analyze return statements in function
The first return statement returns append(left, right...), so the second return is unreachable.Step 2: Identify missing root value append
Root value is never appended because code after first return is ignored.Final Answer:
The root value is appended after the return statement, so never executed -> Option AQuick Check:
Code after return is unreachable [OK]
- Ignoring unreachable code after return
- Not appending root value in correct order
- Confusing base case return value
5
/ \
3 8
\ \
4 10
What is the inorder traversal output?Solution
Step 1: Apply inorder traversal on given tree
Visit left subtree: node 3, then its right child 4, then root 5, then right subtree nodes 8 and 10.Step 2: List nodes in inorder sequence
Sequence is 3, 4, 5, 8, 10 which is sorted ascending order.Final Answer:
[3, 4, 5, 8, 10] -> Option BQuick Check:
Inorder traversal of BST gives sorted nodes [OK]
- Ignoring nodes with single child
- Visiting root before left subtree
- Mixing preorder or postorder with inorder
Ready to prove it?
Pass the test and this pattern moves unknown
