Binary Tree Node Structure in DSA Typescript - Time & Space Complexity
We want to understand how the time to create or access nodes in a binary tree grows as the tree gets bigger.
How does the number of steps change when we add more nodes?
Analyze the time complexity of the following code snippet.
class TreeNode {
value: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(value: number) {
this.value = value;
this.left = null;
this.right = null;
}
}
This code defines a single node in a binary tree with a value and two child pointers.
Since this snippet only creates one node, there are no loops or recursion here.
- Primary operation: Creating one node with fixed steps.
- How many times: Exactly once per node creation.
Each node creation takes the same small number of steps, no matter the tree size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 node creations x constant steps = 10 x c |
| 100 | 100 x c |
| 1000 | 1000 x c |
Pattern observation: The total work grows linearly if we create many nodes one by one.
Time Complexity: O(1) per node creation
This means creating a single node always takes the same small amount of time, no matter what.
[X] Wrong: "Creating a node takes longer as the tree grows bigger."
[OK] Correct: Each node is created independently with fixed steps, so size of the tree does not affect the time to create one node.
Understanding that node creation is a constant-time operation helps you focus on the parts of tree algorithms where time grows with input, like traversals or searches.
"What if the constructor also initialized child nodes recursively? How would the time complexity change?"
Practice
Solution
Step 1: Understand binary tree node components
A binary tree node typically stores a value and references to two children: left and right.Step 2: Compare options with definition
A value, a left child node, and a right child node correctly lists value, left child, and right child. Others describe linked list or general tree nodes.Final Answer:
A value, a left child node, and a right child node -> Option BQuick Check:
Binary tree node = value + left + right [OK]
- Confusing binary tree node with linked list node
- Thinking nodes have multiple children like general trees
- Ignoring child pointers in node structure
Solution
Step 1: Check property types and initialization
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } correctly declares left and right as Node or null and initializes them to null in constructor.Step 2: Verify constructor parameters and assignments
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } constructor takes value and sets left and right to null, matching typical binary tree node setup.Final Answer:
Correct TypeScript class with nullable children and proper constructor -> Option CQuick Check:
Nullable children + constructor with value = class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } [OK]
- Not initializing left and right to null
- Using undefined instead of null for children
- Missing constructor parameter for value
console.log(root.left?.value)?
class Node {
value: number;
left: Node | null;
right: Node | null;
constructor(value: number) {
this.value = value;
this.left = null;
this.right = null;
}
}
const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
console.log(root.left?.value);Solution
Step 1: Analyze node assignments
root.left is assigned a new Node with value 5, so root.left is not null.Step 2: Understand optional chaining and output
Using root.left?.value safely accesses value of left node, which is 5.Final Answer:
5 -> Option AQuick Check:
root.left.value = 5 [OK]
- Confusing null and undefined outputs
- Assuming root.left is null by default
- Misreading optional chaining operator
class Node {
value: number;
left: Node;
right: Node;
constructor(value: number) {
this.value = value;
this.left = null;
this.right = null;
}
}Solution
Step 1: Check property types vs assigned values
left and right are typed as Node but assigned null, causing type error.Step 2: Fix type declarations
Change left and right types to Node | null to allow null assignment.Final Answer:
left and right should allow null type (Node | null) -> Option AQuick Check:
Nullable children fix type error [OK]
- Not updating types when assigning null
- Removing constructor unnecessarily
- Making value optional without reason
Solution
Step 1: Understand parent pointer addition
Adding a parent property of type Node | null stores reference to parent node.Step 2: Impact on traversal
This allows traversing upwards from child to parent, enabling bidirectional navigation.Final Answer:
Add a parent property of type Node | null; allows upward traversal from child to parent -> Option DQuick Check:
Parent pointer enables upward traversal [OK]
- Using wrong type for parent pointer
- Assuming parent pointer exists by default
- Confusing parent pointer with multiple parents
Ready to prove it?
Pass the test and this pattern moves unknown
