Three traversals of binary trees-preorder, middle order, and postorder traversal

Three kinds of binary tree traversal-pre-order, middle-order, and post-order traversal:

Pre-order/pre-order traversal : The order of reading is: root-left-right The
order is: ABDGHCEIF
Insert picture description here
Middle-order traversal : the order of reading is: left-root-right The
order is: GDHBAEICF
Insert picture description here

Post-order traversal : the reading order is: left-right-root, the
order is: GHDBIEFCA

Insert picture description here
For known conditions:
known in-order and post-order: the last node of a post-order binary tree is the root node, and the left and right subtrees of the root of the in-order binary tree are left and right subtrees. In addition, the decomposition can be sorted , As follows,
middle order: DCEF AKJLIM
post-order: DFEC KJMIA
1. Decompose and sort the left subtree with A as the root node:
(1) "DCEF" in the middle order and "DFEC" in the subsequent order can be decomposed and sorted. Sort first, the left is the middle order, the right is the post-order
Insert picture description here
(2) and then the BHG of the middle order and the HGB of the post-order are sorted, the left is the middle order, and the right is the post-order
Insert picture description here
2. Then the right of the root node is A Sort the subtrees:

Insert picture description here
The final result is shown in the figure:
Insert picture description here
the pre-order traversal is: ABCDEFGHIJKLM.
Note: In the case of empty left subtree or right subtree, when starting contact, try to include empty nodes as much as possible, and omit the result without writing it in.

Guess you like

Origin blog.csdn.net/m0_46015143/article/details/105856485