📖 Scenario: You are building a simple binary tree by manually creating nodes and linking them. This is like making a family tree by adding each person and connecting parents to children.
🎯 Goal: Build a binary tree by creating nodes with values and linking left and right children manually. Then print the root node to see the tree structure.
📋 What You'll Learn
Create a class called TreeNode with value, left, and right properties
Create a root node with value 10
Create left and right child nodes with values 5 and 15
Link the child nodes to the root node
Print the root node to show the tree
💡 Why This Matters
🌍 Real World
Binary trees are used in many places like organizing data, searching quickly, and making decisions step-by-step.
💼 Career
Understanding how to build and link nodes in a tree is a key skill for software developers working with data structures and algorithms.
Progress0 / 4 steps
1
Create the TreeNode class
Create a class called TreeNode with a constructor that takes a number value and sets this.value. Initialize this.left and this.right to null.
DSA Typescript
Hint
Think of each node as a box holding a number and two empty spots for left and right children.
2
Create the root and child nodes
Create a variable called root and set it to a new TreeNode with value 10. Then create two more variables called leftChild and rightChild with values 5 and 15 respectively.
DSA Typescript
Hint
Use the new keyword to create nodes with the values given.
3
Link the child nodes to the root
Set root.left to leftChild and root.right to rightChild to link the children to the root node.
DSA Typescript
Hint
Assign the child nodes to the left and right properties of the root.
4
Print the root node
Use console.log(root) to print the root node and see the tree structure.
DSA Typescript
Hint
Use console.log to see the whole tree starting from the root.
Practice
(1/5)
1. What is the main purpose of manually creating a binary tree in TypeScript?
easy
A. To avoid using classes or objects
B. To automatically sort data without coding
C. To understand how nodes connect and form the tree structure
D. To create a linked list instead of a tree
Solution
Step 1: Understand binary tree structure
A binary tree consists of nodes where each node has at most two children, left and right.
Step 2: Purpose of manual creation
Manually creating a binary tree helps visualize and understand how nodes connect to form the tree.
Final Answer:
To understand how nodes connect and form the tree structure -> Option C
D. Assigning number 5 to root.left instead of a Node or null
Solution
Step 1: Check type of root.left assignment
root.left is declared as Node or null, but assigned number 5 which is invalid.
Step 2: Confirm correct assignment type
root.left should be assigned a Node instance or null, not a number.
Final Answer:
Assigning number 5 to root.left instead of a Node or null -> Option D
Quick Check:
Type mismatch in root.left assignment = Assigning number 5 to root.left instead of a Node or null [OK]
Hint: Children must be Node or null, not a number [OK]
Common Mistakes:
Assigning primitive values to node children
Ignoring type errors in assignments
Assuming number can be assigned directly
5. You want to manually create a binary tree with root value 20, left child 10, and right child 30. Which code snippet correctly creates this tree and prints the root, left, and right values in order?
hard
A. const root = new Node(20);
root.left = 10;
root.right = 30;
console.log(root.value, root.left, root.right);
B. const root = new Node(20);
root.left = new Node(10);
root.right = new Node(30);
console.log(root.value, root.left?.value, root.right?.value);
C. const root = new Node(20);
root.left = new Node(30);
root.right = new Node(10);
console.log(root.value, root.left?.value, root.right?.value);
D. const root = new Node(20);
root.left = null;
root.right = null;
console.log(root.left?.value, root.value, root.right?.value);
Solution
Step 1: Create root and children nodes correctly
Root node with value 20, left child 10, right child 30 must be Node instances.
Step 2: Print values in order root, left, right
console.log prints root.value, root.left?.value, root.right?.value which outputs 20 10 30.
Final Answer:
const root = new Node(20);
root.left = new Node(10);
root.right = new Node(30);
console.log(root.value, root.left?.value, root.right?.value); -> Option B
Quick Check:
Correct node creation and print order = const root = new Node(20);
root.left = new Node(10);
root.right = new Node(30);
console.log(root.value, root.left?.value, root.right?.value); [OK]
Hint: Create nodes with new, assign children as nodes, print values [OK]
Common Mistakes:
Assigning numbers directly to children
Swapping left and right child values
Printing children before root or using null children
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.