Pre-order, middle-order and post-order traversal of binary tree (easy-to-understand version)

I understand that the pre-order, middle-order, post-order, etc. all refer to when the root node is traversed

Pre-order traversal is also called first-root traversal or first-order traversal, that is, the root node is traversed first

In the middle order traversal is the left subtree first, and the root node is traversed in the middle order

Post-order traversal is also called post-root traversal, which means that the left and right subtrees are first traversed, and the root node is traversed last (instead of first right subtree, it is still first left subtree)

Preorder traversal: first visit the root node, then traverse the left subtree, and finally traverse the right subtree. When traversing the left and right subtrees, we still visit the root node first, then traverse the left subtree, and finally traverse the right subtree.

Mid-order traversal: first traverse the left subtree, then visit the root node, and finally traverse the right subtree.

Post-order traversal: first traverse the left subtree, then the right subtree, and finally visit the root node. When traversing the left and right subtrees, still first traverse the left subtree, then traverse the right subtree, and finally traverse the root node.

Preorder traversal

The binary tree shown in the figure above:

Preorder traversal result: ABDECF

Intermediate traversal results: DBEAFC

Post-order traversal result: DEBFCA

It is worth noting that it is known that pre-order/post-order traversal + middle-order traversal can reconstruct a binary tree. (Also a very classic algorithm problem)

Guess you like

Origin blog.csdn.net/a1059526327/article/details/108342850