Tree Terminology Root Leaf Height Depth Level in DSA Java - 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.
public int height(Node node) {
if (node == null) return -1;
int leftHeight = height(node.left);
int rightHeight = height(node.right);
return 1 + Math.max(leftHeight, rightHeight);
}
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 gets bigger, the function visits more nodes, so the work grows 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 directly with the number of nodes.
Time Complexity: O(n)
This means the time to find the height grows in a straight line with the number of nodes.
[X] Wrong: "The height function only looks at the longest path, so it runs in less than linear time."
[OK] Correct: Even though height depends on the longest path, the function must visit every node to be sure, so it still checks all nodes.
Understanding how tree operations scale helps you explain your code clearly and shows you know how data size affects performance.
"What if we changed the tree to be a linked list (all nodes have only one child)? How would the time complexity change?"
Practice
root?Solution
Step 1: Understand the root node concept
The root node is the starting point of the tree and has no parent node above it.Step 2: Identify the correct description
Among the options, only the topmost node with no parent matches the root node definition.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 any node at bottom
Node is a class representing tree nodes?Solution
Step 1: Understand leaf node definition
A leaf node has no children, so its left and right child references should be null.Step 2: Check each option for leaf node declaration
Node leaf = new Node(5); // with one child: Node leaf = new Node(5); uses standard constructor which sets left = right = null, creating a leaf node. A adds a child. C likely causes syntax error (no such constructor). D is null, not a node.Final Answer:
Node leaf = new Node(5); // with no children -> Option DQuick Check:
Leaf node has no children = left and right null [OK]
- Assuming leaf nodes must have data only
- Creating children for leaf nodes
- Setting leaf node to null reference
1
/ \
2 3
/ / \
4 5 6
What is the height of the tree?Solution
Step 1: Understand height of a tree
Height is the number of edges on the longest path from root to a leaf node.Step 2: Find longest path from root (1) to leaf
Paths: 1->2->4 has 2 edges, 1->3->5 has 2 edges, 1->3->6 has 2 edges. So height = 2.Final Answer:
2 -> Option CQuick Check:
Longest path edges = 2 [OK]
- Counting nodes instead of edges for height
- Confusing height with depth
- Counting root as height 1
class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
left = right = null;
}
}
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
root.right.left = new Node(60);
root.right.right = new Node(70);
System.out.println(root.left.right.data);Solution
Step 1: Trace node assignments
root.left.right is assigned new Node(50), so it exists and has data 50.Step 2: Check print statement
Printing root.left.right.data will print 50 without error.Final Answer:
No error, prints 50 -> Option BQuick Check:
root.left.right.data = 50 [OK]
- Assuming uninitialized nodes cause error here
- Confusing left and right child nodes
- Thinking print outputs 60
1
/ \
2 3
/ / \
4 5 6
Solution
Step 1: Understand depth definition
Depth is the number of edges from the root node to the target node.Step 2: Trace path from root (1) to node 6
Path: 1 -> 3 -> 6 has 2 edges, so depth is 2.Final Answer:
2 -> Option AQuick Check:
Edges from root to node 6 = 2 [OK]
- Counting nodes instead of edges for depth
- Confusing depth with level starting at 1
- Assuming depth equals height
Ready to prove it?
Pass the test and this pattern moves unknown
