Detailed illustration of the four types of traversal of binary trees (pre-order, in-order, and post-order hierarchical traversal)


In this paper, take this binary tree as an example
insert image description here

1. Preorder traversal

normal operation

root first, then left, then right
insert image description here

The overall structure of the traversal is determined:
insert image description here

insert image description here

Determines the overall structure in the left subtree
insert image description here

Proceed to:

insert image description here

At this point, the traversal in the left subtree has been completed
insert image description here

insert image description here

Determine the overall structure in the right subtree
insert image description here

insert image description here

insert image description here

insert image description here

The preamble ends and
the result is: ABDFECGIJHK
insert image description here

easy way

Start from the root node and go around the binary tree to the left, the order of the nodes passed is the pre-order traversal order
insert image description here
The result is: ABDFECGIJHK

2. Inorder traversal

normal operation

First left, then root, then right
insert image description here

From this, it can be determined that the root A is in the middle, and the order in the left and right subtrees is respectively determined below
insert image description here

In the left subtree:

insert image description here

This determines the order in the left subtree
insert image description here
insert image description here

insert image description here

In the right subtree:
divide the right subtree as a whole
insert image description here
So it can be determined:
insert image description here
continue:
insert image description here
insert image description here

insert image description here

insert image description here

End of inorder traversal
The result is: FDBEAIGJCHK

easy way

Take this binary tree as an example
insert image description here

The result is: FDBEAIGJCHK

3. Post-order traversal

normal operation

first left, then right, then root

insert image description here

Separate the overall structure, and the root node will always be at the last position during post-order traversal

insert image description here

insert image description here

Continue to divide the structure of the left subtree

insert image description here

insert image description here

Since then, the traversal in the left subtree ends

insert image description here

insert image description here

Divide the right subtree structure

insert image description here

insert image description here

insert image description here

insert image description here

insert image description here
end of postorder traversal

The result is: FDEBIJGKHCA

Four. Hierarchical traversal

normal operation

Take each line of the binary tree as a whole, output from top to bottom, and output each line in order from left to right The
insert image description here
first line: A
The second line: BC
The third line: DEGH
The fourth line: FIJK
The result is: ABCDEGHFIJK

Guess you like

Origin blog.csdn.net/m0_68681879/article/details/127847415