Article Directory
In this paper, take this binary tree as an example
1. Preorder traversal
normal operation
root first, then left, then right
The overall structure of the traversal is determined:
Determines the overall structure in the left subtree
Proceed to:
At this point, the traversal in the left subtree has been completed
Determine the overall structure in the right subtree
The preamble ends and
the result is: ABDFECGIJHK
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
The result is: ABDFECGIJHK
2. Inorder traversal
normal operation
First left, then root, then right
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
In the left subtree:
This determines the order in the left subtree
In the right subtree:
divide the right subtree as a whole
So it can be determined:
continue:
End of inorder traversal
The result is: FDBEAIGJCHK
easy way
Take this binary tree as an example
The result is: FDBEAIGJCHK
3. Post-order traversal
normal operation
first left, then right, then root
Separate the overall structure, and the root node will always be at the last position during post-order traversal
Continue to divide the structure of the left subtree
Since then, the traversal in the left subtree ends
Divide the right subtree structure
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
first line: A
The second line: BC
The third line: DEGH
The fourth line: FIJK
The result is: ABCDEGHFIJK