Binary tree traversal understanding

This article focuses on how to traverse a binary tree, because when I encountered a binary tree again before, I found pre-order traversal, in-order traversal, and post-order traversal, and I forgot how to find it. Only the motto remains:

Preorder traversal: root node ---> left subtree ---> right subtree

In-order traversal: left subtree --->  root node ---> right subtree

Post-order traversal: left subtree ---> right subtree  ---> root node

Hierarchical Traversal: Just traverse by hierarchy

After a long time, in fact, the formula has not been written down (at that time, it was not paid enough attention, and it was used less). The key points to remember are:

The traversal of the binary tree is from left to right. The three words front, middle and back represent the position of the root node (in fact, it is also the position of the final root node).

Then, seeing the preorder traversal again, "front" means that the root node is in front, and then from left to right, that is: root node - left subtree - right subtree

 Next, remember the rules, how to find it? As shown below:

 

Hierarchical traversal will not be explained in detail, it is ok from top to bottom, the result is: abcdefghi

Preorder traversal thinking: root - left - right. a, and then the left subtree b, but b also has a left subtree. At this time, b is regarded as the root again, and the next step should be the root b-left d-right e. At this point, we have determined ab. Since d has subtrees, when we reach d, we need to re-count the root d-left h-right i. Since h and i have no subtrees, our order in this step is expanded to: abdhi, The subtree of d has been traversed. At this time, the root b-left d-right e is continued, so add e after the order, and so on, to complete the whole structure, and the final result is: abdhiecfg.

Inorder traversal thinking: left - root - right. At this point, the starting point is the leftmost left subtree h, left h-root d-right i. At this time, I continued the way of thinking of preorder traversal, but the result was: hdbaiefcg, and the middle logic was a bit weird. Why is this? In a hurry to get the answer, I first came up with a rule based on the correct result: to search from the top down, you can use the thinking mode of the previous order, each step is a new starting point, from the bottom to the top, it is to use this part of them as a As a whole, you need to finish walking before you can go up. For example, d, h, and i are regarded as a d point, and the order (in order) in d is hdi. At this time, d is regarded as the left subtree and continues to traverse. According to the law, hdi (internal order of point d), dbe (internal order of point b), and the last found order: hdibeafcg.

Post-order traversal thinking: left-right-root. Combined with the rules obtained from the middle order, hid is regarded as the internal order of point d, and the final search result is: hidebfgca.

The general rule: top to bottom, a new starting point. From bottom to top, the root is the mouth. (This port is an interface, the things in the interface have their own order, and the entire interface is sorted with the outside)

Well, the method is obtained, and the law is summarized. Why are the ways of thinking different between the pre-order, the middle-order, and the post-order? On the surface, naturally, one from top to bottom, one from bottom to top...

In-depth theoretical knowledge, I hope the great gods can answer it.

Decided to write down more of your own thinking patterns in the future... It should be beneficial and harmful (time-consuming), but looking back many years later, the most important thing in that year should be time, right?

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324507360&siteId=291194637