Binary (preamble, in sequence, after traversing picture Detailed steps)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_42651904/article/details/90288715

First, we have such a binary tree:
Here Insert Picture Description

  • Preorder traversal: root -> left sub-tree -> right subtree (first traversal of the root node, then left and right)

Preorder traversal of this tree is: ABDEGHCF

  • Preorder: left subtree -> root -> right sub-tree (in the middle of the root node traversal)

Preorder traversal of this tree is: DBGEHACF

  • Postorder: left sub-tree -> right subtree -> root (last traversing root)

Preorder traversal of this tree is: DGHEBFCA

  • Traverse the level: hierarchical traversal

Preorder traversal of this tree is: ABCDEFGH

ps: the so-called preamble, in sequence, subsequent,That is, in terms of the root node, About the same order of traversal, the preamble is the first root node traversal, then the left and right; inorder traversal is in the middle of the root node; the sequence is the root node on the last traverse.


  • T questions: prior known binary tree traversal order is: ABDEGHCF, inorder traversal of: DBGEHACF, seeking after preorder

analysis:

  1. First, we traverse the root node as seen from the preamble A
  2. Is known as the root node A, Central preorder left subtree is found DBGEH, right subtree is CF

After determining these two points it is easy to calculate the original binary tree the same again.
We see the right sub-tree node to CF, preorder is CF, then it can be inferred that appear to the right in the binary tree looks like this:
Here Insert Picture Description
Why F is not left subtree of it, because if you change the order of F in the left, in order traversal FC into the

Traversed by the preamble AB may know, the left subtree A is definitely B, so now the tree is this:
Here Insert Picture Description
re-order traversal Central DB seen, D to B's left subtree
Here Insert Picture Description

Now only EGH not determined
first of all we have to determine is certainly not sub-tree D, and if so, preorder it would not be a DB
by the preorder traversal node E can only be seen right subtree of the B
Here Insert Picture Description
, the final Central preorder traversal of the complete binary tree GEH known as:
Here Insert Picture Description

Inferred after traversing the entire tree to another is very easy to write out.

The key to this problem is to determine the root and the left and right subtrees. If the latter is known to order traversal, is the same as the last one is the root node.

Guess you like

Origin blog.csdn.net/qq_42651904/article/details/90288715