Tree Terminology Root Leaf Height Depth Level in DSA Go - Time & Space Complexity
We want to understand how the time to work with trees changes as the tree grows.
How does the size and shape of a tree affect how long operations take?
Analyze the time complexity of the following code snippet.
// Simple function to find the height of a binary tree
func height(node *Node) int {
if node == nil {
return 0
}
leftHeight := height(node.Left)
rightHeight := height(node.Right)
if leftHeight > rightHeight {
return leftHeight + 1
}
return rightHeight + 1
}
This code finds the height of a tree by checking the height of left and right subtrees recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Once per node in the tree.
As the tree grows, the function visits every node once to find height.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: Operations grow directly with the number of nodes.
Time Complexity: O(n)
This means the time to find the height grows in direct proportion to the number of nodes.
[X] Wrong: "The height function only looks at the longest path, so it runs in constant time."
[OK] Correct: Even though height depends on the longest path, the function must visit every node to be sure, so time grows with total nodes.
Understanding how tree operations scale helps you explain your approach clearly and confidently in interviews.
"What if the tree was balanced versus very unbalanced? How would that affect the time complexity of finding height?"
Practice
root?Solution
Step 1: Understand the root node definition
The root node is the starting point of a tree and has no parent node above it.Step 2: Differentiate root from other nodes
Leaf nodes have no children, but root specifically has no parent. Lowest level nodes are leaves or internal nodes, not necessarily root.Final Answer:
The topmost node with no parent -> Option AQuick Check:
Root = topmost node with no parent [OK]
- Confusing root with leaf node
- Thinking root must have children
- Assuming root is at the lowest level
Solution
Step 1: Recall Go pointer syntax
In Go, pointers are declared with * before the type, e.g., *Node.Step 2: Check each option's pointer usage
type Node struct { data int, left *Node, right *Node } uses *Node correctly. type Node struct { data int, left Node, right Node } uses Node (value, not pointer). type Node struct { data int, left &Node, right &Node } uses &Node which is invalid syntax. type Node struct { data int, left Node*, right Node* } uses Node* which is invalid in Go.Final Answer:
type Node struct { data int, left *Node, right *Node } -> Option BQuick Check:
Pointer syntax in Go = *Type [OK]
- Using value type instead of pointer for children
- Using &Type or Type* which are invalid in Go
- Using semicolons instead of commas in struct fields
type Node struct {
data int
left *Node
right *Node
}
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}
Solution
Step 1: Understand depth definition
Depth of a node is the number of edges from the root to that node.Step 2: Trace path from root to node 5
Path: 1 (root) -> 2 -> 5. Number of edges = 2.Final Answer:
2 -> Option AQuick Check:
Depth = edges from root = 2 [OK]
- Counting nodes instead of edges
- Confusing depth with height
- Miscounting path length
type Node struct {
data int
left *Node
right *Node
}
func isLeaf(n *Node) bool {
return n.left == nil && n.right == nil
}
root := &Node{data: 10}
root.left = &Node{data: 20}
root.right = &Node{data: 30}
root.left.left = &Node{data: 40}
fmt.Println(isLeaf(root.left))
What is the output and why?Solution
Step 1: Understand isLeaf function
It returns true if both left and right children are nil (no children).Step 2: Check root.left node children
root.left has left child (40) but no right child, so left != nil, right == nil.Step 3: Evaluate isLeaf(root.left)
Since left is not nil, isLeaf returns false.Final Answer:
false -> Option DQuick Check:
Leaf = no children, root.left has left child [OK]
- Assuming node with one child is leaf
- Confusing nil checks causing panic
- Expecting true without checking both children
Solution
Step 1: Calculate height of node at level 3
Height = total tree height - node level = 5 - 3 = 2.Step 2: Identify node at level 0
By definition, root node is at level 0.Final Answer:
Height = 2; Root node is at level 0 -> Option CQuick Check:
Height = 5 - 3 = 2; Root at level 0 [OK]
- Confusing height with level
- Assuming leaf node at level 0
- Calculating height as level number
Ready to prove it?
Pass the test and this pattern moves unknown
