Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Preorder traversal method of binary tree in c language

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "the preorder traversal method of c language binary tree". The explanation content in this article is simple and clear, and it is easy to learn and understand. let's study and learn the preorder traversal method of c language binary tree.

Title

Given a binary tree, return its preorder traversal.

Example:

Input: [1meme nullpene 2pas 3]

Output: [1Jing 2jue 3]

Advanced: the recursive algorithm is very simple, can you do it through the iterative algorithm?

Problem-solving ideas

Deal with the problem with the idea of Stack.

The order of preorder traversal is root-left-right, and the specific algorithm is:

Push the root node to the stack

Loop to check whether the stack is empty, and if not, take out the top element of the stack and save its value

Check whether its right child node exists, and if so, push to the stack

Look at its left child node, if it exists, then push to the stack.

Animation demonstration

The animation shows that GIF loads a little slowly. Please wait a moment to load and display ^ _ ^.

Reference code

Supplement

The following writing method uses an auxiliary node p, this writing method can actually be regarded as a template, corresponding to the middle order and post-order template writing, the form is very unified, convenient for memory. This method will be supplemented in subsequent updates in both the preface and post-preface articles. The ideas and codes are as follows:

The auxiliary node p is initialized to the root node, and the condition of the while loop is that the stack is not empty or the auxiliary node p is not empty.

In the loop, it is first judged that if the auxiliary node p exists, then p is added to the stack, and then the node value of p is added to the result res, where p points to its left child node.

Otherwise, if p does not exist, it indicates that there is no left child node. We take out the top node of the stack and point p to the right child node of the top node of the stack.

Thank you for your reading. the above is the content of "the preorder traversal method of c language binary tree". After the study of this article, I believe you have a deeper understanding of the preorder traversal method of c language binary tree. the specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report