The following code lists the nodes in a binary tree in two different orders:
preorder:C,B,A,E,D,F,H
postorder:A,B,D,H,F,E,C
Draw the binary tree.
The answer is :
C
/ \
B E
/ / \
A D F
/
H
so, my question is how should i think or is there any quick method to get the above answer?I know how to draw a binary tree by its sequence but become confuse in the other way round just like this question.Please help me as soon as possible.thanks.
xonxon
-4
Light Poster
Recommended Answers
Jump to PostIf you want an algorithm, I'd recommend looking for patterns in the traversal results.
For example, the root is the first hit in preorder and the last hit in postorder. You can go from left to right in preorder and find the left parent vine (C, B, A in …
Jump to PostThere would seem to be method in such madness. But, as Narue said, you cannot entirely solve the tree without an inorder traversal as well.
The problem should be solved recursively, as you might expect.
pre: C B A E D F H post: A B …
All 5 Replies
Narue
5,707
Bad Cop
Team Colleague
nucleon
114
Posting Pro in Training
nucleon
114
Posting Pro in Training
dgkris
0
Newbie Poster
mrnutty
761
Senior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.