📖 Scenario: Imagine you are building a simple family tree app. Each person can have up to two children. To represent this, you will create a binary tree node structure in JavaScript.
🎯 Goal: You will create a binary tree node structure with a value and two child nodes (left and right). Then, you will create a sample node and print its value.
📋 What You'll Learn
Create a class called TreeNode with a constructor that takes a value parameter
The constructor should set this.value to the given value
The constructor should initialize this.left and this.right to null
Create a variable called root that is an instance of TreeNode with value 10
Print the value of root using console.log
💡 Why This Matters
🌍 Real World
Binary trees are used in many applications like organizing data, searching quickly, and representing hierarchical information such as family trees or file systems.
💼 Career
Understanding how to create and manipulate tree structures is important for software development roles that involve data organization, algorithms, and system design.
Progress0 / 4 steps
1
Create the TreeNode class
Create a class called TreeNode with a constructor that takes a parameter value. Inside the constructor, set this.value to value, and set this.left and this.right to null.
DSA Javascript
Hint
Use class keyword and define a constructor method. Initialize this.value, this.left, and this.right.
2
Create the root node
Create a variable called root and assign it a new instance of TreeNode with the value 10.
DSA Javascript
Hint
Use const root = new TreeNode(10); to create the root node.
3
Add left and right children
Add two children to the root node. Set root.left to a new TreeNode with value 5 and root.right to a new TreeNode with value 15.
DSA Javascript
Hint
Use root.left = new TreeNode(5); and root.right = new TreeNode(15); to add children.
4
Print the root node value
Print the value of the root node using console.log(root.value).
DSA Javascript
Hint
Use console.log(root.value); to print the root node's value.
Practice
(1/5)
1. What does a typical binary tree node contain in its structure?
easy
A. A value and multiple child nodes in a list
B. A value, a left child node, and a right child node
C. Only a value and a pointer to the next node
D. Only a value without any child references
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 B
Quick Check:
Binary tree node = value + left + right [OK]
Hint: Binary tree nodes always have left and right child pointers [OK]
Common Mistakes:
Confusing binary tree node with linked list node
Thinking nodes have multiple children like general trees
Ignoring child pointers in node structure
2. Which of the following is the correct JavaScript syntax to define a binary tree node class?
easy
A. class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } }
B. function Node(value) { this.value = value; this.next = null; }
C. class Node { constructor(value) { this.value = value; this.children = []; } }
D. var Node = (value) => { this.value = value; this.left = null; this.right = null; }
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 A
Quick Check:
Class with value, left, right = class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } [OK]
Hint: Use ES6 class with left and right set to null [OK]
A. function countNodes(node) { if (node === null) return null; return 1 + countNodes(node.left) + countNodes(node.right); }
B. function countNodes(node) { if (!node) return 1; return countNodes(node.left) + countNodes(node.right); }
C. function countNodes(node) { return node.value + countNodes(node.left) + countNodes(node.right); }
D. function countNodes(node) { if (!node) return 0; return 1 + countNodes(node.left) + countNodes(node.right); }
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 D
Quick Check:
Count nodes = 1 + left count + right count [OK]
Hint: Return 0 for null, else 1 + left + right counts [OK]
Common Mistakes:
Returning 1 for null node instead of 0
Adding node.value instead of count 1
Returning null instead of 0 in base case
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.