Binary Tree Node Structure in DSA Javascript - Time & Space Complexity
We want to understand how the time to create and link nodes in a binary tree grows as we add more nodes.
How does the work change when the tree gets bigger?
Analyze the time complexity of the following code snippet.
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
const root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);
This code creates a simple binary tree node structure and links child nodes to the root.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating and linking each node.
- How many times: Once per node created; no loops or recursion here.
Each new node requires a fixed amount of work to create and link.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 node creations and links |
| 100 | 100 node creations and links |
| 1000 | 1000 node creations and links |
Pattern observation: The work grows directly with the number of nodes added.
Time Complexity: O(n)
This means the time to build the tree grows linearly with the number of nodes.
[X] Wrong: "Creating a node takes constant time, so the whole tree is always O(1)."
[OK] Correct: While one node is quick, building many nodes adds up, so total time grows with the number of nodes.
Understanding how node creation scales helps you reason about tree operations and their costs in real problems.
"What if we added a loop to create nodes instead of individual statements? How would the time complexity change?"
Practice
Solution
Step 1: Understand binary tree node components
A binary tree node stores a value and has exactly two child references: left and right.Step 2: Compare options with binary tree 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: Identify correct class syntax for binary tree node
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } uses ES6 class with constructor setting value, left, and right to null.Step 2: Check other options for errors
function Node(value) { this.value = value; this.next = null; } uses next pointer (linked list), C uses children array (general tree), D uses arrow function incorrectly for constructor.Final Answer:
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } -> Option AQuick Check:
Class with value, left, right = class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } [OK]
- Using 'next' instead of 'left' and 'right'
- Using arrow function as constructor
- Using children array instead of left/right
class Node {
constructor(value) {
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 + ' -> ' + root.value + ' -> ' + root.right.value);Solution
Step 1: Trace node creation and assignments
Root node value is 10, left child is 5, right child is 15.Step 2: Evaluate console.log output
Output concatenates left.value (5), root.value (10), right.value (15) with arrows.Final Answer:
"5 -> 10 -> 15" -> Option AQuick Check:
Left 5, root 10, right 15 = "5 -> 10 -> 15" [OK]
- Mixing order of values in output
- Confusing left and right child values
- Forgetting to access .value property
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
const root = new Node(20);
root.left = new Node(10);
root.right = new Node(30);
root.left.left = 15;
console.log(root.left.left.value);Solution
Step 1: Analyze assignment to root.left.left
root.left.left is assigned 15 (a number), not a Node instance.Step 2: Understand why console.log fails
Accessing .value on a number (15) causes error because 15 has no .value property.Final Answer:
Assigning a number directly instead of a Node object to left.left -> Option CQuick Check:
Child nodes must be Node objects, not raw values [OK]
- Assigning raw values instead of Node objects
- Ignoring error from accessing .value on number
- Assuming constructor is missing
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}Choose the correct function implementation.
Solution
Step 1: Understand base case for recursion
If node is null, count is zero because no node exists there.Step 2: Sum current node plus counts of left and right subtrees
Return 1 (current node) plus recursive counts of left and right children.Step 3: Check options for correct base case and sum
function countNodes(node) { if (!node) return 0; return 1 + countNodes(node.left) + countNodes(node.right); } correctly returns 0 for null and sums 1 + left + right counts.Final Answer:
function countNodes(node) { if (!node) return 0; return 1 + countNodes(node.left) + countNodes(node.right); } -> Option DQuick Check:
Count nodes = 1 + left count + right count [OK]
- Returning 1 for null node instead of 0
- Adding node.value instead of count 1
- Returning null instead of 0 in base case
Ready to prove it?
Pass the test and this pattern moves unknown
