offer Notes of Sword Fingers - 5. Mirror Image of Binary Tree

Keywords: Python

5. Mirror image of binary tree
Operate a given binary tree and transform it into a mirror image of the source binary tree.
Mirror Definition of Binary Tree: Source Binary Tree
8
/
6 10
/ \ /
5 7 9 11
Mirror Binary Tree
8
/
10 6
/ \ /
11 9 7 5
Solution 1:

#class TreeNode:
#    def __init__(self,x):
#        self.val = x
#        self.left = None
#        self.right = None
class Solution:
        def Mirror(self,root):
                if root != None:
                        root.left, root.right = root.right,root.left
                        self.Mirror(root.left)
                        self.Mirror(root.right)

Solution 2:

class Solution:
        def Mirror(self,root):
                if not root:
                        return root
                if not root.left and not root.right:
                        return root
                if root.left or root.right:
                        t = root.right
                        root.right = root.left
                        root.left = root.right
                self.Mirror(root.left)
                self.Mirror(root.right)
                return root

Matrix operation knowledge in Python:
https://www.cnblogs.com/chamie/p/4870078.html

5. Clockwise Printing Matrix
Input a matrix and print out each number in clockwise order from the outside to the inside. For example, if you input the following 4X 4 matrix: 1 23 4 5 6 7 8 9 11 12 13 14 15 16, print out the numbers 1, 2, 3, 4, 8, 12, 16, 15, 13, 9, 5, 6, 7, 11, 10.

Extended Question: Counterclockwise Printing Matrix

Note: This is difficult, the process of thinking is very complex, and not necessarily correct, discussion area answers, good topics, practice has learned a lot ~Thank the gods for their answers.
Solution 1: ranking second, has been concerned about the answer of a god, the idea is to take the first row of the matrix, the remaining matrix row and column exchange, and then row flip, the resulting matrix takes the first row, matrix.pop(0), the remaining matrix row and column exchange, row flip, until the remaining matrix is no longer a matrix.

# -*- coding:utf-8 -*-
class Solution:
    # matrix type is a two-dimensional list, which needs to be returned
    def printMatrix(self, matrix):
        # write code here
        result=[]
        while matrix:
            result=result+matrix.pop(0)
            if not matrix:
                break
            matrix=self.turn(matrix)
        return result
    def turn(self, matrix):
        r=len(matrix)
        c=len(matrix[0])
        B=[]
        for i in range(c):
            A=[]
            for j in range(r):
                A.append(matrix[j][i])
            B.append(A)
        B.reverse()
        return B

The xrange function is used in the first place of the solution, because it is in Python 2, not familiar with, not understand, so let go.~

Summary: The speed is too slow, but a good topic learns a lot. Come on.~
Strive to write out the extended questions and paste them on~

Posted by easethan on Fri, 05 Apr 2019 23:15:29 -0700