LC442 Find All Duplicates in an Array

```python class Solution: #time O(N) space O(N) def findDuplicates(self, nums: List[int]) -> List[int]: d = {} res = [] for num in nums: if num not in d: d[num] = True else: res.append(num) return res [Read More]
Tags: Array

LC99 Recover Binary Search Tree

```python 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: “”” main idea: for a BST, if we do in order traversal, current node’s val >= prev node’s val; since there must be a pair... [Read More]
Tags: BST DFS

LC9 Palindrome Number

```python class Solution: time O(N) space O(N) main idea: string the number and compare from both end ; edge case: num <0 false def isPalindrome(self, x: int) -> bool: if x < 0: return False s = str(x) l = 0 r = len(s)-1 while l < r: if s[l]... [Read More]
Tags: Math

LC103 Binary Tree Zigzag Level Order Traversal

# 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 zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]: level = 0 res = [] if not root: return res q = collections.deque()... [Read More]
Tags: Tree BFS

LC342 Power of Four

```python class Solution: def isPowerOfFour(self, num: int) -> bool: # time O(N/4) if num <= 0 : return False power = 0 x = 0 while x < num : x = 4**power if x == num: return True power += 1 return False [Read More]