0
0
DSA Javascriptprogramming~10 mins

Binary Tree Node Structure in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a binary tree node with a value property.

DSA Javascript
class TreeNode {
  constructor(val) {
    this.val = [1];
    this.left = null;
    this.right = null;
  }
}
Drag options to blanks, or click blank then click option'
Aval
Bvalue
Cnode
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name like 'value' instead of 'val'.
Forgetting to assign the value to this.val.
2fill in blank
medium

Complete the code to create a new TreeNode with value 5.

DSA Javascript
const node = new TreeNode([1]);
Drag options to blanks, or click blank then click option'
Anull
Bundefined
C'5'
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Passing '5' as a string instead of the number 5.
Passing null or undefined instead of a value.
3fill in blank
hard

Fix the error in the code to correctly assign the left child of a node.

DSA Javascript
node.[1] = new TreeNode(3);
Drag options to blanks, or click blank then click option'
Aright
Bchild
Cleft
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning to 'right' instead of 'left'.
Using an incorrect property name like 'child' or 'node'.
4fill in blank
hard

Fill both blanks to check if a node has a right child and assign it to a variable.

DSA Javascript
if (node.[1] !== [2]) {
  const rightChild = node.right;
}
Drag options to blanks, or click blank then click option'
Aright
Bnull
Cleft
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'left' instead of 'right'.
Comparing to undefined instead of null.
5fill in blank
hard

Fill all three blanks to create a node, assign left and right children with values 1 and 2.

DSA Javascript
const root = new TreeNode([1]);
root.[2] = new TreeNode(1);
root.[3] = new TreeNode(2);
Drag options to blanks, or click blank then click option'
A0
Bleft
Cright
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'root' as a property name instead of 'left' or 'right'.
Assigning children to wrong properties.