Machine-translated by Google. Questions are authored in English.
Top

Discussion

Given a binary tree with :

(i) The inorder tree traversal output as : A B C D
(ii) The preorder tree traversal output as : C B A D

Then the original binary tree is :

  • A.Root A; B is the left child of A, C the right child of A, and D the right child of C
  • B.Root A; B is the left child of A, C the right child of A, and D the left child of C
  • C.Root C; B is the left child of C, D the right child of C, and A the left child of B
  • D.Root A; C is the left child of A, D the left child of C, and B the left child of D

Answer: C

The tree has root C, with B as its left child, D as its right child, and A as the left child of B.

Preorder lists the root first, so C is the root. In the inorder sequence A B C D, everything before C (A, B) is in the left subtree and D is in the right subtree. In the left subtree the preorder is B A, so B is its root, and A comes before B in inorder, so A is B's left child.

Check: inorder A B C D and preorder C B A D both match. The trees rooted at A would give a preorder starting with A, so options A, B and D are wrong.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.