Binary tree reconstruction

 Our question is:

Given a binary tree preorder and inorder traversal, how to build a binary tree do?

We know that the first point preorder traversal of a binary tree traversal must be the root node, the root node must be A.

In order traversal, the root node must be the most in the middle of the node A in preorder the left in the left subtree of A, the right node in the right subtree of A.

Then we spawned a sub-question:

If a binary tree preorder as: B D E H I, preorder as: D B H E I, to construct a binary tree of what. This lesson two binary tree is constructed out of the left subtree A nodes.

Clearly, A root node is a left subtree of the node B, the node B and the left node is D, a right sub-tree includes nodes H, E, I, and we know the preorder preorder right subtree.

We then construct the right subtree of a node B, E visible root node, the left node E is H, a right node I.

Similarly, we can construct A node of the right subtree:

So finally out of the binary tree is constructed:

Learning video link

Guess you like

Origin www.cnblogs.com/zhengchang/p/erchashuchonggou.html