Postorder traversal sequence preorder traversal front

Today saw a number of issues concerning peace to find a binary tree, by the way also brushed up the binary tree traversal rules, learn to write about the document.

Tree traversal order roughly divided into three: a preorder traversal of (roots first traversal, preorder), inorder traversal (in preorder traversal), postorder traversal (after Traversal).

As illustrated binary tree:

Preorder traversal: preorder traversal can be written about the root, if the binary tree is empty, return at the end.

Preorder traversal rules:

(1) root access

(2) preorder traversal left subtree

(3) preorder traversal right subtree

It should be noted: the complete step 2 and 3 when the rule is to traverse a binary tree is completed in accordance with the preamble.

Output preorder traversal of: ABDECF

Preorder: preorder can be written as the root of the left and right, which means that the binary tree traversal, we must first traverse the left subtree binary tree, then traverse the root node, and finally traversing the right subtree.

Similarly, when the binary tree is empty, the end of the return.

In order traversal rules:

(1) preorder left subtree

(2) access to the root

(3) in order to traverse the right subtree

Note: In step 3, when completed, should be done in accordance with the rules of order traversal.

Output inorder traversal of: DBEAFC

Postorder: postorder can be written about the root, which means that the binary tree traversal, the first traversal of the left subtree in accordance with the rules of the post-order traversal, then traverse the right subtree accordance with the rules after traversing the last access to the root node.

When binary tree is empty, the end of the return.

Postorder binary tree rules:

(1) postorder left subtree

(2) preorder right subtree

(3) access to the root

Note: In step 1, when completed, is still to be done according to the rules after traversing.

Postorder output order: DEBFCA

Guess you like

Origin www.cnblogs.com/haoran333/p/12633995.html
Recommended