#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)