Traversal of binary tree--pre-order, middle order, post order, sequence

Source: Internet
Author: User

The traversal reason of binary tree: the form of Sequence programming diagram or binary tree is really very intuitive. However, the final treatment is to the computer, the computer processing only judgment, circulation, etc., that is, can only process the antecedent sequence. The traversal of the binary tree is the linear sequence of the tree structure of the sequence, and the linear sequence is given to the computer processing.

The traversal of a binary tree is broadly divided into four types: pre-sequence traversal, middle sequence traversal, post-order traversal, and sequence traversal.

Pre-order traversal (top-down): Starts at the root node and takes root node values, traverses all Zuozi of the root node and all nodes of Zuozi, and then traverses the right subtree of the root node. It is important to note that the traversal of the binary tree uses the idea of recursion, that is, each node is the root node, for example, the root node of the 0 layer is the root node of the 1-layer node, if the binary tree has 2,3 .... Layer node, then the 1 layer is the root node of the 2-tier node.

Middle sequence traversal (bottom-up): The root node begins without taking the root node value, which is to traverse all the left subtree nodes of the root node, one at a time to the root node, and then to the right subtree traversal, thinking and recursion of the pre-order traversal is the same.

Post-order traversal: left-to-right first leaves after the node to traverse the way to access the word and left, the last access to the root node.

Sequence traversal: is a layer of traversal.

Summary: All are from left to right, first left subtree after right subtree, just traverse the way different. It is best to summarize the law through graphs.
Take a piece from "Big tree structure".

Traversal of binary tree--pre-order, middle order, post order, sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.