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 convert python binary Tree into binary Tree Mirror

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

Share

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

This article is about how to convert a python binary tree into a binary tree image. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Binary tree mirror image problem

Manipulate the given binary tree and transform it into a mirror image of the source binary tree.

Train of thought

First traverse, nodes into the stack, and then out of the stack to swap the left and right nodes

Change the left and right nodes in the process of traversal

Code #-*-coding:utf-8-*-class TreeNode: def _ _ init__ (self, x): self.val = x self.left = None self.right = None#-*-coding:utf-8-*-# class TreeNode:# def _ _ init__ (self, x): # self.val = x # self.left = None# self.right = Noneclass Solution: # traverse first Enter the stack and change the left and right nodes def Mirror (self, root): # determine whether the incoming node is empty if root is None: return None line_node = self.printLevelNode (root) # print (line_node) while line_node: tmp = line_node.pop () if tmp.left or tmp.right: tmp.left Tmp.right = tmp.right, tmp.left return tmp # hierarchy traverses the binary tree, which is called def printLevelNode (self Root): line_node = [] res = [] line_node.append (root) while line_node: tmp = line_node.pop (0) res.append (tmp) if tmp.left: line_node.append (tmp.left) if tmp.right: line_ In the process of traversing the node.append (tmp.right) return res # hierarchy, the left and right nodes def Mirror2 (self) are exchanged Root): if root is None: return None line_node = [] line_node.append (root) while line_node: tmp = line_node.pop (0) if tmp.left: line_node.append (tmp.left) if tmp.right: line_node.append ( Tmp.right) # if tmp.left or tmp.right: tmp.left Tmp.right = tmp.right, tmp.left return root # swap the left and right nodes def Mirror3 (self, root): if root is None: return None root.left, root.right = root.right during recursive traversal Root.left self.Mirror3 (root.left) self.Mirror3 (root.right) return rootif _ _ name__ ='_ _ main__': node1 = TreeNode (8) node2 = TreeNode (6) node3 = TreeNode (10) node4 = TreeNode (5) node5 = TreeNode (7) node6 = TreeNode (9) node7 = TreeNode (11) node1.left = node2 node1.right = node3 node2.left = Node4 node2.right = node5 node3.left = node6 node3.right = node7 sl = Solution () ls = sl.Mirror3 (node1) print (ls) for i in sl.printLevelNode (ls): print (i.val) above is how to convert a python binary tree into a binary tree image The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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