What will be post order traversal of a binary tree T, if preorder and inorder…
2022
What will be post order traversal of a binary tree T, if preorder and inorder traversals of T are given by ABCDEF and BADCFE respectively?
- A.
BDFECA
- B.
BCFDEA
- C.
BFDECA
- D.
BEFDCA
Attempted by 370 students.
Show answer & explanation
Correct answer: A
Step 1: In preorder traversal, the first element is the root of the tree. Here, the preorder is ABCDEF, so A is the root.
Step 2: In inorder traversal, elements to the left of the root belong to the left subtree, and elements to the right belong to the right subtree. The inorder is BADCFE, so the left subtree has B, A, D and the right subtree has C, F, E.
Step 3: The left subtree (BAD) has B as the root in preorder (next element after A), so B is the left child of A. In inorder, B is to the left of A, so B is the leftmost node. D is to the right of B in inorder, so D is the right child of B.
Step 4: The right subtree (CFE) has C as the root in preorder. In inorder, C is followed by F and E, so C is the root, F is the left child, and E is the right child.
Step 5: Postorder traversal is left-right-root. Traverse the left subtree (B → D → B), then the right subtree (C → F → E → C), then the root (A).
Final postorder: BDFECA.
हिन्दी उत्तर:
चरण 1: प्री-ऑर्डर में पहला तत्व वृक्ष का मूल होता है। यहाँ प्री-ऑर्डर ABCDEF है, इसलिए A मूल है।
चरण 2: इन-ऑर्डर में मूल के बाईं ओर के तत्व बाईं उप-वृक्ष में होते हैं, और दाईं ओर के तत्व दाईं उप-वृक्ष में होते हैं। इन-ऑर्डर BADCFE है, इसलिए बाईं उप-वृक्ष में B, A, D हैं और दाईं उप-वृक्ष में C, F, E हैं।
चरण 3: बाईं उप-वृक्ष (BAD) में B प्री-ऑर्डर में A के बाद आता है, इसलिए B A का बाईं बच्चा है। इन-ऑर्डर में B A के बाईं ओर है, इसलिए B सबसे बाईं नोड है। D B के दाईं ओर है, इसलिए D B का दाईं बच्चा है।
चरण 4: दाईं उप-वृक्ष (CFE) में C प्री-ऑर्डर में मूल है। इन-ऑर्डर में C के बाद F और E हैं, इसलिए C मूल है, F बाईं बच्चा है, और E दाईं बच्चा है।
चरण 5: पोस्ट-ऑर्डर ट्रैवर्सल बाईं-दाईं-मूल होता है। बाईं उप-वृक्ष (B → D → B) का ट्रैवर्सल करें, फिर दाईं उप-वृक्ष (C → F → E → C) का ट्रैवर्सल करें, फिर मूल (A)।
अंतिम पोस्ट-ऑर्डर: BDFECA।