1. 程式人生 > >劍指offer:18. 19. 20. 21 (12.27)

劍指offer:18. 19. 20. 21 (12.27)

#101. 對稱二叉樹(leetcode)

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if not root:
            return True
        else:
            return self.duichen(root.left,root.right)
    def duichen(self,root1,root2):
        if not root1 and not root2:
            return True
        if not root1 or not root2:
            return False
        return  root1.val==root2.val and self.duichen(root1.left,root2.right) and self.duichen(root1.right,root2.left)
'''
18.二叉樹的映象
題目: 操作給定的二叉樹,將其變換為源二叉樹的映象。
'''
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
        
class Solution:
    # 返回映象樹的根節點
    def Mirror(self, root):
        if root is None:
            return
        if root.left is None and root.right is None:
            return
        temp=root.left
        root.left=root.right
        root.right=temp

        if root is not None:
            self.Mirror(root.left)
        if root is not None:
            self.Mirror(root.right)
if __name__=='__main__':
    A1 = TreeNode(8)
    A2 = TreeNode(6)
    A3 = TreeNode(10)
    A4 = TreeNode(5)
    A5 = TreeNode(7)
    A6 = TreeNode(9)
    A7 = TreeNode(11)
    A1.left=A2
    A1.right=A3
    A2.left=A4
    A2.right=A5
    A3.left=A6
    A3.right=A7

    temp1=[]
    solution=Solution()
    solution.order_traversal(A1,temp1)
    print(temp1)
    solution.Mirror(A1)
    solution.order_traversal(A1,temp1)
    print(temp1)
'''
19.順時針列印矩陣
輸入一個矩陣,按照從外向裡以順時針的順序依次打印出每一個數字,
例如,如果輸入如下矩陣:
 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 16
則依次打印出數字
1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
'''

class Solution:
    # matrix型別為二維列表,需要返回列表
    def printMatrix(self, matrix):
        # write code here
        m,n=len(matrix),len(matrix[0])   #計算行數和列數
        res = []
        if n==1 and m==1:
            res.append(matrix[0][0])
            return res
        for k in range((min(m,n)+1)//2):   #跑的圈數
            [res.append(matrix[k][i]) for i in range(k, n - k)]
            [res.append(matrix[j][n-k-1]) for j in range(k,m-k) if matrix[j][n-k-1] not in res]
            [res.append(matrix[m-k-1][j]) for j in range(n-k-1,k-1,-1) if matrix[m-k-1][j] not in res]
            [res.append(matrix[j][k]) for j in range(m-1-k,k-1,-1) if matrix[j][k] not in res]
        return res

if __name__=='__main__':
    solution=Solution()
    m,n=1,5
    matrix=[]
    for i in range(0,m):
        matrix.append(list(map(int,input().split(' '))))
    print(matrix)
    ans=solution.printMatrix(matrix)
    print(ans)
'''
20.包含Min函式的棧
**題目:**定義棧的資料結構,請在該型別中實現一個能夠得到棧中所含最小元素的min函式。
'''

class Solution:
    def __init__(self):
        self.num=[]
    def push(self, node):
        self.num.append(node)   #定義了一個棧
    def pop(self):
        self.num.pop()
    def top(self):
        numlen = len(self.num)
        return self.num[numlen-1]
    def min(self):
        return min(self.num)

if __name__=='__main__':
    solution = Solution()
    solution.push(1)
    solution.push(2)
    solution.push(3)
    solution.push(4)
    solution.pop()
    print(solution.top())
    print(solution.min())
'''
21.棧的壓入彈出序列
**題目:**輸入兩個整數序列,第一個序列表示棧的壓入順序,請判斷第二個序列是否可能為該棧的彈出順序。
假設壓入棧的所有數字均不相等。例如序列1,2,3,4,5是某棧的壓入順序,序列4,5,3,2,1是該壓棧序列對應的一個彈出序列,
但4,3,5,1,2就不可能是該壓棧序列的彈出序列。(注意:這兩個序列的長度是相等的)。

**題解:**新構建一箇中間棧,來模擬棧的輸入和棧的輸出,比對輸入結果和輸出結果是否相等。

'''
class Solution:
    def IsPopOrder(self, pushV, popV):
        if len(pushV)==1 and len(popV)==1 and pushV[0]!=popV[0]:
            return False

        helpV=[]
        pushV.reverse()
        popV.reverse()
        #模擬給定棧的壓入和壓出
        helpV.append(pushV[len(pushV)-1])
        pushV.pop()
        while True:
            if helpV[len(helpV)-1]!=popV[len(popV)-1]:
                helpV.append(pushV[len(pushV)-1])
                pushV.pop()

            if helpV[len(helpV)-1]==popV[len(popV)-1]:
                helpV.pop()
                popV.pop()

            if pushV==[] and popV==[] and helpV==[]:
                return True

            if pushV==[] and popV[len(popV)-1]!=helpV[len(helpV)-1]:
                return False


if __name__=='__main__':
    solution=Solution()
    push=list(map(int,input().split(' ')))
    pop=list(map(int,input().split(' ')))
    ans=solution.IsPopOrder(push,pop)
    print(ans)