In-order Traversal (Recursive) - Binary Tree
To do an in-order traversal of a binary tree recursively, we just use the definition of in-order traversal: starting at the root, visit the left subtree in-order, then visit root, then visit the right subtree in-order:
void inorder(Node *root) {
if (root == nullptr) return;
inorder(root->left);
cout << root->value << '\n';
inorder(root->right);
}
Time complexity
The time complexity is O(n) (where n is the number of nodes in the tree) because that's the total work done when we combine the work done by each recursive call. See the video for more detail.
Space complexity
The space complexity is O(h) (where h is the height of the tree) because of the space taken by the call stack. See the video for more detail.