November 20, 2024
Today I chose an easy problem since I had a late day at work and gym afterwards.
1from collections import deque23# Definition for a binary tree node.4# class TreeNode:5# def __init__(self, val=0, left=None, right=None):6# self.val = val7# self.left = left8# self.right = right9class Solution:10 def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:11 pQ = deque([p])12 qQ = deque([q])1314 while pQ and qQ:15 nodeP = pQ.popleft()16 nodeQ = qQ.popleft()17 if nodeP and nodeQ and nodeP.val != nodeQ.val:18 return False19 elif (nodeP is None) != (nodeQ is None):20 return False2122 if nodeP:23 pQ.append(nodeP.left)24 pQ.append(nodeP.right)25 if nodeQ:26 qQ.append(nodeQ.left)27 qQ.append(nodeQ.right)2829 if not pQ and not qQ:30 return True3132 return False
Time complexity is O(n) = n and space complexity is O(n).
LeetCode 25. Reverse Nodes in k-Group
LeetCode 82. Remove Duplicates from Sorted List II