799 words
4 minutes
日拱两卒(七)
2026-08-02
40
二叉树的直径。
这里的坑是长度是按边计算的,不是节点数。
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: ans = 0
def dfs(cur) -> int: nonlocal ans if cur is None: return 0 l = dfs(cur.left) + 1 r = dfs(cur.right) + 1 nonlocal ans ans = max(ans, l + r - 2) return max(l, r)
dfs(root) return ans39
对称二叉树。
不能拍成中序遍历看回文,是有反例的。
class Solution: def isSame(self, p, q) -> bool: if p is None or q is None: return p is q if p.val != q.val: return False return self.isSame(p.left, q.right) and self.isSame(p.right, q.left)
def isSymmetric(self, root: Optional[TreeNode]) -> bool: return self.isSame(root.left, root.right)38
翻转二叉树。
简单递归。
class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if root is None: return None tmp = deepcopy(root.right) root.right = self.invertTree(root.left) root.left = self.invertTree(tmp) return root37
二叉树的最大深度。
简单递归。
class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: if root is None: return 0 return max(self.maxDepth(root.left) + 1, self.maxDepth(root.right) + 1)36
二叉树的中序遍历。
简单递归。
class Solution: def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: ans = []
def dfs(cur): if cur is None: return dfs(cur.left) ans.append(cur.val) dfs(cur.right)
dfs(root)
return ans35
LRU缓存。
维护一个每个节点含有KV信息的双向链表+一个用于快速定位的哈希表。
Python竟然有现成的数据结构OrderedDict。
class LRUCache:
def __init__(self, capacity: int): self.capacity = capacity self.cache = OrderedDict()
def get(self, key: int) -> int: if key not in self.cache: return -1 self.cache.move_to_end(key, last=False) return self.cache[key]
def put(self, key: int, value: int) -> None: self.cache[key] = value self.cache.move_to_end(key, last=False) if len(self.cache) > self.capacity: self.cache.popitem()34
合并K个升序链表。
递归左右两部分再合二为一。
class Solution: def mergeTwo(self, left: Optional[ListNode], right: Optional[ListNode]) -> Optional[ListNode]: dummy = ListNode() cur = dummy
while left and right: if left.val < right.val: cur.next = left left = left.next else: cur.next = right right = right.next cur = cur.next cur.next = left if left else right
return dummy.next
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: k = len(lists) if k == 0: return None if k == 1: return lists[0] left = self.mergeKLists(lists[: k // 2]) right = self.mergeKLists(lists[k // 2 :]) return self.mergeTwo(left, right)33
排序链表。
感觉写起来很烦,一看题解确实要写这么烦。
class Solution: def get_middle(self, head: Optional[ListNode]) -> Optional[ListNode]: if head is None: return None
slow = head fast = head while fast.next and fast.next.next: fast = fast.next.next slow = slow.next
res = slow.next slow.next = None return res
def mergeTwo(self, left: Optional[ListNode], right: Optional[ListNode]) -> Optional[ListNode]: dummy = ListNode() cur = dummy
while left and right: if left.val < right.val: cur.next = left left = left.next else: cur.next = right right = right.next cur = cur.next cur.next = left if left else right
return dummy.next
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: if head is None or head.next is None: return head
middle = self.get_middle(head)
left = self.sortList(head) right = self.sortList(middle)
return self.mergeTwo(left, right)32
随机链表的复制。
目前题干最莫名其妙的一题。
深拷贝每个节点多一个随机指针的链表,可以用哈希表解决。题解做法也是目前最出乎意料,每个节点原地复制,然后拆出最后的结果。
class Solution: def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]': cur = head while cur: cur.next = Node(cur.val, cur.next) cur = cur.next.next
cur = head while cur: if cur.random: cur.next.random = cur.random.next cur = cur.next.next
cur = dummy = Node(0, head) while cur.next: cur.next = cur.next.next cur = cur.next
return dummy.next31
K个一组翻转链表。
有些繁琐。
class Solution: def reverse(self, head: Optional[ListNode]) -> Optional[ListNode]: prev = None cur = head while cur: nxt = cur.next cur.next = prev prev = cur cur = nxt return prev
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]: dummy = ListNode(0, head)
cur = dummy.next cnt = 0 cur_head = cur pre_cur_head = dummy while cur: cnt += 1 nxt = cur.next if cnt >= 2: cur.next = None new_head = self.reverse(cur_head) cur_head.next = nxt pre_cur_head.next = new_head
pre_cur_head = cur_head cur_head = nxt cnt = 0
cur = nxt
return dummy.next