Old Wei wins the offer to take you to learn --- Brush title series (18 binary tree mirroring)

18. The binary image

problem:

Operation of a given binary tree, the binary tree is converted into a source image.

Here Insert Picture Description

solve:

thought:

This question we need to observe, binary tree is actually a mirror image of the left and right child nodes exchange node, then we write a recursive can be achieved so that the overall binary image up.

python code:

# -*- 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):
            root.left,root.right=root.right,root.left
            self.Mirror(root.left)
            self.Mirror(root.right)
Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/104911746