# What is the mnemonic for traversing a binary tree

### Is there any pattern to binary tree traversal?

Pre-order mid-order post order. The preorder traversal order is root node, left subtree, right subtree; the midorder traversal order is left subtree, root node, right subtree; the postorder traversal order is left subtree, right subtree, root node.

### Data structures binary tree traversal way student collection

Data structures computer science majors must learn binary tree traversal

Precedential traversal

Precedential traversal can be imagined as a small person from the root node of a binary tree as the starting point, along the outer edges of the binary tree. Counterclockwise walk around back to the root node, the order of the elements encountered on the way, is the result of the prior order traversal. The order of the elements encountered on the way back to the root is the result of a priori traversal. Coincidence: left root right

The result of the middle traversal is: HDIBEJAFKCG

Post-order traversal

Post-order traversal is like cutting grapes, we want to cut a bunch of grapes into one by one. Circle around the outside of the tree, and if you find a grape (it has to be a single grape) that you can cut with a single pair of scissors (i.e., the grapes have to fall one by one, not more than 1 in a single breath), cut it off, and the composition will be a backward-order traversal.

Cleverly remember: left and right root

Post-order traversal results: HIDJEBKFGCA

Hierarchical traversal

Hierarchical traversal is very good to understand, it is the root node from the beginning, layer by layer, from top to bottom, from left to right, in each layer, in turn, to write the value of the can be. Note: Traversing all nodes goes to the left child first, then to the right child.

Hierarchical traversal result: ABCDEFGHIJK

### Binary Tree (2) – Traversing a Binary Tree

Feel free to share any questions! Microblogging @HelloWorld-_-

Then create a binary tree article to say a few ways to traverse the binary tree

As we all know, the traversal of binary trees is divided into four categories, respectively, for the first order traversal, middle order traversal, back order traversal and hierarchical traversal. I remember when I was learning about binary trees, the first order traversal, middle order traversal and backward traversal is always confusing (class did not listen carefully), forget the order of access, and then summarized two points, and then never forget, the following to introduce my memory tips

Precedential traversal: the root, the left subtree, the right subtree

Middle order traversal: the left subtree, the root, the right subtree

Post-order traversal: the left subtree, the root, the right subtree

Post-order traversal: the left subtree, the root, the right subtree

Post-order traversal: left subtree, right subtree, root

Well here’s an example that describes the process of traversing a binary tree.

The easiest thing to understand is hierarchical traversal, where each node is accessed in order from top to bottom, left to right. Here’s a diagram to show the hierarchical traversal order of a binary tree

First, recall the rules of prior traversal: root, left subtree, right subtree. According to the rules, first visit the root A, then the left child node B, then visit B, not directly to visit the right child node C of A, because the left child tree of A has not been visited, so the following node to visit is D. Node D has no children nodes, so then visit the node E. When you visit the node E, the whole left child tree of the root A visit is complete, and then start to visit the right child node C of A, the latter visit order is the same as node B.

What is the order of access to the root A, and then visit the right child node C?

Recall the rules of middle-order traversal: left subtree, root, right subtree. According to the rules, first judge node A, because A is the root node, so next judge its left child node B. Node B is also the root node relative to node D, E, so continue to judge node B’s left child node D, node D is a leaf node, so the first visit is node D. After visiting node D, node B’s left child tree are visited, and then visit node B, node E immediately after. After visiting node E, the left subtrees of node A are visited, then node A is visited, followed by the right subtree of node A in the same way.

Post-order traversal rules: left subtree, right subtree, root. Ditto the analysis process for middle-order traversal.