Complete the code to visit the left subtree first in inorder traversal.
void inorderTraversal(struct Node* root) {
if (root == NULL) return;
inorderTraversal(root->[1]);
printf("%d ", root->data);
inorderTraversal(root->right);
}In inorder traversal, we first visit the left child, so root->left is correct.
Complete the code to print the root node's data in inorder traversal.
void inorderTraversal(struct Node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%[1] ", root->data);
inorderTraversal(root->right);
}The format specifier for integers in printf is %d.
Fix the error in the recursive call to traverse the right subtree in inorder traversal.
void inorderTraversal(struct Node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->[1]);
}After visiting the root, inorder traversal visits the right subtree using root->right.
Fill both blanks to complete the inorder traversal function correctly.
void inorderTraversal(struct Node* root) {
if (root == NULL) return;
inorderTraversal(root->[1]);
printf("%d ", root->data);
inorderTraversal(root->[2]);
}In inorder traversal, first visit the left child, then the root, then the right child.
Fill all three blanks to create a function that returns the inorder traversal as a string of node values separated by spaces.
#include <stdio.h> #include <stdlib.h> #include <string.h> void inorderTraversalStr(struct Node* root, char* result) { if (root == NULL) return; inorderTraversalStr(root->[1], result); char buffer[12]; sprintf(buffer, "%[2] ", root->data); strcat(result, buffer); inorderTraversalStr(root->[3], result); }
The function visits left child, appends the root data with %d format, then visits right child.
