Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to convert python binary Tree into binary Tree Mirror

Shulou Source: shulou.com Published: 2022-06-01 14:11:46 10月03日 Update

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.

Tags: Nodes mirrors processes hierarchies more knowledge articles utf-8 utility code that is work sessions ideas articles look knowledge points industry details information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Shulou Tech Info Microsoft macOS