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

Python implements sequence traversal code sharing in binary tree with non-recursion

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "Python uses non-recursion to achieve order traversal code sharing in binary tree". In daily operation, I believe many people have doubts about Python using non-recursion to achieve order traversal code sharing in binary tree. Editor consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the doubt that "Python uses non-recursion to achieve order traversal code sharing in binary tree". Next, please follow the editor to study!

In fact, the middle order traversal is to find the leftmost node, then the parent node, and then to the right node of the parent node.

For example, the following traversal result in the middle order is DBEAFC

Non-recursive implementation of logic, I think this is relatively stupid. Use a queue as the stack, first press it into the stack according to the left traversal; when you reach the left leaf node, read and delete the association; push the stack back to the upper node, and if the superior node does not have a right node, read and delete; if so, traverse the right node; to prevent the parent node from being read again when the right traversal returns; to record the last pushed node, if it is the right node, the parent node information is not read.

The code is ugly, not to be carved, see laugh.

# Definition for a binary tree node.# class TreeNode:# def _ init__ (self, x): # self.val = x # self.left = None# self.right = Noneclass Solution: def inorderTraversal (self, root: TreeNode)-> List [int]: traversalList = [] nodeList = [] # similar as Preorder traversal, the only change is that the value of node is recored when the node doesn't have left sub-node New object removedNode as popped node, if a node's right sub-node is removedNode, then it should be popped both. If root! = None: nodeList.append (root) currentNode = root removedNode = None while nodeList! = []: if currentNode.left! = None: currentNode = currentNode.left nodeList.append (currentNode) elif currentNode.right = = None or currentNode.right = = removedNode: If currentNode.right = = None: traversalList.append (currentNode.val) removedNode = nodeList.pop () if nodeListrates = []: currentNode = nodeList [- 1] currentNode.left = None elif currentNode.right! = None: TraversalList.append (currentNode.val) currentNode = currentNode.right nodeList.append (currentNode) return traversalList so far The study on "Python uses non-recursion to realize order traversal code sharing in binary tree" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report