献芹奏曝-Python面试题-算法-树篇
上一篇:献芹奏曝-Python面试题
开篇的话:本文目的是收集和归纳力扣上的算法题,希望用python语言,竭我所能做到思路最清奇、代码最简洁、方法最广泛、性能最高效,了解常见题目,找到最利于记忆的答案,更加从容的应对面试。希望广思集益,共同进步。
树篇
-
104. 二叉树的最大深度(难度系数✯)
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: if not root: return 0 return max(self.maxDepth(root.left),self.maxDepth(root.right))+1
运行结果:1:耗时超过92%。2:内存超过48%
知识点/技巧: 把下一位赋值给当前
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: _result = 0 # 创建结果存储变量 # 深度优先 def maxDepth(self, root: Optional[TreeNode]) -> int: self.DFS(0,root) return self._result # 定义递归函数(终止条件、自身调用) def DFS(self,cur_result,root: Optional[TreeNode]): # 终止条件 if root: cur_result +=1 # 更新当前层级 if not root.left and not root.right: self._result = max(self._result,cur_result) return self.DFS(cur_result,root.left) self.DFS(cur_result,root.right)
- 98. 验证二叉搜索树
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isValidBST(self, root: Optional[TreeNode]) -> bool: return self.is_valid_BST(root,-2**31-1,2**31) def is_valid_BST(self, root, min_val, max_val): print(root,min_val,max_val) if not root: return True if root.val >= max_val or root.val <= min_val: return False return self.is_valid_BST(root.left,min_val,root.val) and self.is_valid_BST(root.right,root.val,max_val)
运行结果:1:耗时超过46%。2:内存超过82%
知识点/技巧: 递归调用,注意【5,4,6,null,null,3,7】
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: prev = None def isValidBST(self, root: Optional[TreeNode]) -> bool: if not root: return True # 访问左子树 if not self.isValidBST(root.left): return False if self.prev and self.prev.val >= root.val: return False self.prev = root # 访问右子树 if not self.isValidBST(root.right): return False return True
运行结果:1:耗时超过46%。2:内存超过8%
知识点/技巧: 利用中序遍历的有序性
- 101. 对称二叉树
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def is_symmetric(self,left,right): if not left and not right: return True if not left or not right or left.val != right.val: return False return self.is_symmetric(left.left,right.right) and self.is_symmetric(left.right,right.left) def isSymmetric(self, root: Optional[TreeNode]) -> bool: if not root: return True return self.is_symmetric(root.left,root.right)
运行结果:1:耗时超过62%。2:内存超过88%
知识点/技巧: 利用递归比对
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: if not root: return True # 通过队列实现 python中虽没有队列,可通过list模拟 先进先出 pop(0) 实现 que = [] que.extend((root.left,root.right)) while que: left = que.pop(0) right = que.pop(0) if not left and not right: continue if not left or not right or left.val != right.val: return False que.extend((left.left,right.right,left.right,right.left)) return True
运行结果:1:耗时超过62%。2:内存超过94%
知识点/技巧: 模拟队列实现
- 102. 二叉树的层序遍历
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: result = [] if not root: return result que = [[root]] while que: cur_que = que.pop(0) que_con = [] res_con = [] for node in cur_que: if node: que_con.extend((node.left,node.right)) res_con.append(node.val) if que_con: que.append(que_con) result.append(res_con) return result
运行结果:1:耗时超过10%。2:内存超过96%
知识点/技巧: 模拟队列实现