Theory
Tree traversal means visiting every node in a tree according to a particular order. The three common depth-first traversals are preorder, inorder and postorder. These traversals are important for processing and searching tree structures.
Example
function inorder(node) {
if (!node) return;
inorder(node.left);
console.log(node.value);
inorder(node.right);
}
Expected Output
The function visits left subtree, current node and right subtree.
Practice
Implement preorder and postorder traversal for a binary tree.
Interview Questions
- What is tree traversal?
- What is inorder traversal?
- What is preorder traversal?
- What is postorder traversal?