Tree and Its Traversal

Source: Internet
Author: User

Tree)
A tree, as its name implies, looks like a tree, but we usually draw a fallen tree with its roots and leaves under it. If you don't say that much, you can see the following figure:

Of course, after the introduction of the tree, we have to introduce some concepts of the tree. I still try to use these concepts with graphs. Who will remember so many words?

During interviews, we often get a binary tree structure. Of course, binary trees are also a kind of tree, it has two sub-nodes except leaf nodes:


As a result, we can also launch the "Three Tree ":


Of course, there are also "Four Forks", "Five Forks", and "Six Forks "...... But it is too difficult to draw. There are too many nodes, and it is skipped.

Traverse a tree (traversal)
It is worth mentioning that it is a binary tree, because it is indeed quite special, the node has two Subtrees, the two Subtrees are divided between the left and right, upside down, is a different binary tree, therefore, the left and right cannot be reversed.


When talking about the "team" in the third article, we mentioned breadth-first traversal. In addition to breadth-first traversal, we also mentioned depth-first traversal ), depth-first traversal can be divided into preorder traversal, postorder traversal, and inorder traversal, explain these traversal types:


Okay, nowCodeStage: write code. I have read many textbooks on data structures. binary tree traversal is essential, and all I know is implemented using recursion. Now, I want you to skip recursion, implements sequential traversal of Binary Trees. What should I do? I will give you a tip: We used the team to traverse the breadth first. We used the * stack * to traverse the middle order *. Let's see if it can be written:

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.