Complete the code to print the left subtree first in inorder traversal.
void inorderTraversal(Node* root) {
if (root == nullptr) return;
inorderTraversal(root->[1]);
std::cout << root->data << " ";
inorderTraversal(root->right);
}In inorder traversal, we first visit the left child, then the root, and finally the right child.
Complete the code to print the root node's data in inorder traversal.
void inorderTraversal(Node* root) {
if (root == nullptr) return;
inorderTraversal(root->left);
std::cout << root->[1] << " ";
inorderTraversal(root->right);
}The root's data is printed between visiting the left and right subtrees in inorder traversal.
Fix the error in the inorder traversal function to correctly visit the right subtree.
void inorderTraversal(Node* root) {
if (root == nullptr) return;
inorderTraversal(root->left);
std::cout << root->data << " ";
inorderTraversal(root->[1]);
}After visiting the root, inorder traversal visits the right subtree, so the pointer should be 'right'.
Fill both blanks to complete the inorder traversal function that prints nodes in left-root-right order.
void inorderTraversal(Node* root) {
if (root == nullptr) return;
inorderTraversal(root->[1]);
std::cout << root->[2] << " ";
inorderTraversal(root->right);
}In inorder traversal, visit left child first, then print the root's data.
Fill all three blanks to complete the inorder traversal function visiting left, root, then right.
void inorderTraversal(Node* root) {
if (root == nullptr) return;
inorderTraversal(root->[1]);
std::cout << root->[2] << " ";
inorderTraversal(root->[3]);
}The inorder traversal visits the left child, then prints the root's data, then visits the right child.