Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name like 'value' instead of 'val'.
Forgetting to assign the value to this.val.
✗ Incorrect
The constructor parameter is named 'val', so we assign this.val = val to store the node's value.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing '5' as a string instead of the number 5.
Passing null or undefined instead of a value.
✗ Incorrect
We pass the number 5 to the TreeNode constructor to create a node with value 5.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning to 'right' instead of 'left'.
Using an incorrect property name like 'child' or 'node'.
✗ Incorrect
The left child of a binary tree node is assigned to the 'left' property.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'left' instead of 'right'.
Comparing to undefined instead of null.
✗ Incorrect
We check if node.right is not null to confirm the right child exists.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'root' as a property name instead of 'left' or 'right'.
Assigning children to wrong properties.
✗ Incorrect
We create root with value 0, assign left child to 'left', and right child to 'right'.