In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Python how to achieve hierarchical traversal of binary trees", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python how to achieve hierarchical traversal of binary trees" bar!
Python implements a hierarchical traversal of binary trees, which looks a little complicated. As shown in the following figure, if a binary tree has multiple layers, each layer is traversed from left to right from top to bottom.
Each layer is placed in a queue, and the queue of multiple queues is returned as a whole.
It looks complicated, but the implementation is actually very simple; without stack, put the current level node with a node queue, traverse the current level node, put the read value into a read queue, and read the children of the current node into another queue. When the traversal of the current queue node is complete, using the next-layer node correspondence is equivalent to entering the next layer, traversing the newly generated child node queue, and so on until the node queue is listed as empty.
The code is as follows
# Definition for a binary tree node.# class TreeNode:# def _ init__ (self, x): # self.val = x # self.left = None# self.right = Noneclass Solution: def levelOrder (self Root: TreeNode)-> list [list]: traversalList = [] nodeList = [] if root! = None: nodeList.append (root) while nodeList! = []: currentLevelVal = [] currentNodeList = [] for node in nodeList: currentLevelVal.append (node.val) ) if node.left! = None: currentNodeList.append (node.left) if node.right! = None: currentNodeList.append (node.right) traversalList.append (currentLevelVal) nodeList = currentNodeList return traversalList Thank you for your reading The above is the content of "how to achieve hierarchical traversal of binary trees by Python". After the study of this article, I believe you have a deeper understanding of how to achieve hierarchical traversal of binary trees by Python, and the specific use needs to be verified in 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.