[Offer] to prove safety binary tree image - Python realization

Description] [Title
operation of a given binary tree, the binary tree is converted into a source image.
Here Insert Picture Description
Solving ideas] [
recursive mirroring operation, from the root to the leaf nodes left and right subtrees can sequentially exchange, implemented in Python code is as follows:

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回镜像树的根节点
    def Mirror(self, root):
        # write code here
        if root == None:
            return
        root.left, root.right = root.right, root.left
        self.Mirror(root.left)
        self.Mirror(root.right)
        return root
        
Published 45 original articles · won praise 0 · Views 729

Guess you like

Origin blog.csdn.net/qq_36643449/article/details/104882529