0
0
DSA Javascriptprogramming~20 mins

Binary Tree Node Structure in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Tree Node Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Binary Tree Node Creation
What is the output of the following JavaScript code that creates a simple binary tree node and prints its structure?
DSA Javascript
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);

console.log(JSON.stringify(root));
A{"value":10,"left":{"value":5,"left":null,"right":null},"right":{"value":15,"left":null,"right":null}}
B{"value":10,"left":5,"right":15}
C{"value":10,"left":null,"right":null}
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Think about how the TreeNode class stores left and right children as objects, not just values.
Predict Output
intermediate
2:00remaining
Value of Left Child Node
After running this code, what is the value of the left child node of the root?
DSA Javascript
function TreeNode(value) {
  this.value = value;
  this.left = null;
  this.right = null;
}

const root = new TreeNode(20);
root.left = new TreeNode(10);
root.right = new TreeNode(30);

const leftValue = root.left.value;
console.log(leftValue);
A10
B20
C30
Dnull
Attempts:
2 left
💡 Hint
Look at what value is assigned to root.left.
🔧 Debug
advanced
2:00remaining
Identify the Error in Binary Tree Node Assignment
What error will this code produce when run?
DSA Javascript
class TreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(5);
root.left.value = 3;
console.log(root.left.value);
Aundefined
B3
CTypeError: Cannot set property 'value' of null
DSyntaxError
Attempts:
2 left
💡 Hint
Check if root.left is initialized before setting its value.
Predict Output
advanced
2:00remaining
Output of Nested Binary Tree Node Structure
What is the output of this code that creates a nested binary tree and prints the root's right child's left child's value?
DSA Javascript
class TreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(1);
root.right = new TreeNode(2);
root.right.left = new TreeNode(3);

console.log(root.right.left.value);
A1
B3
Cnull
D2
Attempts:
2 left
💡 Hint
Trace the path from root to right child, then to left child.
🧠 Conceptual
expert
3:00remaining
Number of Nodes in a Binary Tree
Given a binary tree node structure where each node has 'left' and 'right' pointers, what is the total number of nodes after executing this code?
DSA Javascript
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);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(7);
root.right.right = new TreeNode(18);
A4
B5
C7
D6
Attempts:
2 left
💡 Hint
Count each node created including root and all children.