Recursion: time O(N) space O(H) –> O(N) when tree not even # 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 invertTree(self, root: TreeNode) -> TreeNode: # main idea:...
[Read More]
706. Design HashMap
```python
class ListNode:
def init(self,key,val):
self.pair = (key,val)
self.next = None
[Read More]
70. Climbing Stairs
Brute Force/Recursion: time O(2^N) space O(H)
```python
class Solution:
def climbStairs(self, n: int) -> int:
def dfs(i,n):
if i > n:
return 0
if i == n:
return 1
return dfs(i+1,n) + dfs(i+2,n)
return dfs(0,n)
[Read More]
832. Flipping an Image
time O(N) space O(1) ```python class Solution: def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]: row = len(A) col = len(A[0]) # reverse for i in range(row): for j in range(col//2): A[i][j] , A[i][col-1-j] = A[i][col-1-j] , A[i][j] # invert for i in range(row): for j in range(col): if A[i][j] == 1:...
[Read More]
593. Valid Square
Main idea: 4 sides is equal and two diagonals are equal=> using combination with Count time O(1) space O(1) ```python class Solution: def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool: def dis(p1,p2): return (p2[1] - p1[1])(p2[1] - p1[1]) + (p2[0] - p1[0])(p2[0] - p1[0]) li =...
[Read More]