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

How to analyze the Intermediate order traversal of python binary Tree

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to analyze the middle order traversal of python binary tree. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Mid-order traversal of binary trees

Title

Given a binary tree, return its mid-order traversal.

Example:

Input: [1meme nullpene 2pas 3]

Output: [1, 3, 3, 2]

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 traversal in the middle order is left-root-right. The specific algorithm is as follows:

Starting from the root node, press the root node into the stack first

Then press all its left child nodes into the stack, remove the top node of the stack, and save the node value

Then move the current pointer over its right child node, and if there is a right child node, all its left child nodes can be pressed into the stack in the next loop

Animation demonstration

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

Reference code

Supplement

The following method of writing uses an auxiliary node p, which can actually be regarded as a template, corresponding to the template writing of pre-order and post-order, which is very unified in form and convenient for memory. This method of writing will be supplemented both in the previous update preface and in the subsequent post-preface article. 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 first added to the stack, 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, then add the node value of p to the result res, and point p to the right child node of the top node of the stack.

After reading the above, do you have any further understanding of how to parse the middle order traversal of the python binary tree? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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