Consider the label sequences obtained by the following pairs of traversals on a labeled binary tree. Which of these pairs identify a tree uniquely?
-
preorder and postorder
-
inorder and postorder
-
preorder and inorder
-
level order and postorder
View worked solution
Answer: B
To uniquely reconstruct a binary tree, we must be able to determine the root of each subtree and partition the remaining nodes into its left and right subtrees.
- Inorder traversal is crucial because it provides the relative left-to-right order of the nodes. For any root node, all elements appearing before it in the inorder sequence belong to its left subtree, and all elements appearing after it belong to its right subtree.
- Preorder traversal gives the root of the tree first, while Postorder traversal gives the root of the tree last.
Using these properties:
- Inorder and Postorder (II): The last element of the postorder sequence is the root. We locate this root in the inorder sequence to split the tree into left and right subtrees. This process is applied recursively, identifying the tree uniquely.
- Preorder and Inorder (III): The first element of the preorder sequence is the root. We locate this root in the inorder sequence to split the tree into left and right subtrees. This also reconstructs the tree uniquely.
Why other pairs fail:
- Preorder and Postorder (I): If a node has only one child, we cannot determine whether it is a left child or a right child. For example, a tree with root and left child has preorder and postorder . A tree with root and right child also has preorder and postorder .
- Level order and Postorder (IV): Similarly, we cannot uniquely distinguish left and right subtrees. For example, a tree with root and left child has level order and postorder , which is identical to a tree with root and right child .
Thus, only pairs II and III can uniquely identify a binary tree.
Free practice · Your answers on this page are not saved.
