剑指Offer(Python语言)面试题27,28,29

mac2025-11-12  7

面试题27:二叉树的镜像

题目:请完成一个函数,输入一棵二叉树,该函数输出它的镜像。

二叉树的镜像定义:源二叉树              8            /  \           6   10          / \  / \         5  7 9 11         镜像二叉树             8            /  \           10   6          / \  / \         11 9 7  5 Python代码:

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)

面试题28:对称的二叉树

题目:请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。例如,在图中所示的3棵二叉树中,第一棵二叉树是对称的,而另外两棵不是。

思路:

定义一种遍历算法,先遍历右子节点再遍历左子节点。

Python语言描述:

class TreeNode: def __init__(self, x): self.val =x self.left =None self.right =None class Solution: def isSymmetrical(self, pRoot): if not pRoot: return True return self.recursiveTree(pRoot.left, pRoot.right) def recursiveTree(self, left, right): if not left and not right: return True if not left or not right: return False if left.val ==right.val: return self.recursiveTree(left.left, right.right) and self.recursiveTree(left.right, right.left) return False

面试题29:顺时针打印矩阵

题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。

class Solution: # matrix类型为二维列表,需要返回列表 #剑指offer面试题:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字 - 博客 http://blog.csdn.net/yanxiaolx/article/details/52254590 def printMatrix(self, matrix): # write code here if matrix==[[]]: return row=len(matrix) column=len(matrix[0]) left=0 right=column-1 top=0 boom=row-1 res=[] while right>left and top<boom: #从左到右 for i in range(left,right+1): res.append(matrix[top][i]) #从上到下 for i in range(top+1,boom+1): res.append(matrix[i][right]) #从右到左 for i in range(right-1,left-1,-1): res.append(matrix[boom][i]) #从下到上 for i in range(boom-1,top,-1): res.append(matrix[i][left]) left+=1 right-=1 top+=1 boom-=1 #剩下一行 if boom==top and left<right: for i in range(left,right+1): res.append(matrix[boom][i]) #剩下一列 if left==right and boom>top: for i in range(top,boom+1): res.append(matrix[i][left]) #剩下一个 if boom==top and left==right: res.append(matrix[left][top]) return res # -*- coding:utf-8 -*- class Solution:     # matrix类型为二维列表,需要返回列表     def printMatrix(self, matrix):         # write code here         if matrix==[[]]:             return         row=len(matrix)         column=len(matrix[0])         start=0         res=[]         while row>start*2 and column>start*2:             endX=column-1-start             endY=row-1-start             #从左到右打印一行             for i in range(start,endX+1):                 res.append(matrix[start][i])                 #print(matrix[start][i])             #从上往下打印一列             if start<endY:                 for i in range(start+1,endY+1):                     res.append(matrix[i][endX])                     #print(matrix[i][endX])             #从右到左打印一行             if start<endX and start<endY:                 for i in range(endX-1,start-1,-1):                     res.append(matrix[endY][i])                     #print(matrix[endY][i])             #从下到上打印一列             if start<endX and start<endY-1:                 for i in range(endY-1,start,-1):                     res.append(matrix[i][start])                     #print(matrix[i][start])             start+=1         return res         

 

最新回复(0)