Complete the code to visit the root node first in preorder traversal.
function preorder(node) {
if (node == null) return;
console.log(node.value);
preorder(node[1]);
}In preorder traversal, after visiting the root, we visit the left child first.
Complete the code to visit the right child after the left child in preorder traversal.
function preorder(node) {
if (node == null) return;
console.log(node.value);
preorder(node.left);
preorder(node[1]);
}After visiting the root and left subtree, preorder visits the right subtree.
Fix the error in the preorder traversal code to correctly visit root, left, then right.
function preorder(node) {
if (node == null) return;
console.log(node.value);
preorder(node.left);
preorder(node[1]);
}The second recursive call must visit the right child to follow preorder traversal order.
Fill both blanks to complete the preorder traversal visiting root, left, then right.
function preorder(node) {
if (node == null) return;
console.log(node[1]);
preorder(node[2]);
preorder(node.right);
}First print the node's value, then recursively visit the left child, then the right child.
Fill all three blanks to complete preorder traversal: print root, visit left, then right.
function preorder(node) {
if (node == null) return;
console.log(node[1]);
preorder(node[2]);
preorder(node[3]);
}Print the node's value, then visit left child, then right child for preorder traversal.