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 parse the post-order traversal of python binary tree

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to parse the post-order traversal of python binary tree, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Post-order traversal of binary trees

Title

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

Example:

Input: [1meme nullpene 2pas 3]

Output: [3, 2, 2, 1]

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 traversal order is left-right-root, and the specific algorithm is as follows:

Push the root node into the stack first, and then define an auxiliary node head

The condition of a while loop is that the stack is not empty

In the loop, the top node t of the stack is taken out first

If the top node of the stack has no left or right child node, or its left child node is head, or its right child node is head. We add the top node value of the stack to the result res, move the top element off the stack, and then point the head to the top element of the stack.

Otherwise, if the right child node is not empty, add it to the stack

If the left node is not empty, join 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 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 added in the previous updated preface and the last updated mid-preface article. The ideas and codes are as follows:

First change the root-left-right order of the preorder traversal to root-right-left.

Then flip into the left-right-root of the traversal in the post-order, flip or change the order in which the resulting res is added.

Then change the left and right order of the updated auxiliary node p

This is the answer to the question about how to analyze the post-order traversal of the python binary tree. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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