Invert Binary Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Queue를 이용한 풀이 from collections import deque class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if not root: return None if not root.left and not root.right: return root q = deque([]) q.append(roo..
Linked List Cycle - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Object id값과 집합을 이용한 풀이 class Solution: def hasCycle(self, head: ListNode) -> bool: id_set = set() cur = head while cur: if id(cur) not in id_set: id_set.add(id(cur)) else: return True cur = cur.next return False..
Maximum Subarray - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. prefix sum을 이용한 풀이 class Solution: def maxSubArray(self, nums: List[int]) -> int: if len(nums) == 1: return sum(nums) prefix = [nums[0]] for index, num in enumerate(nums): if index == 0: continue prefix.append(nu..
Symmetric Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. in-order 접근(실패) class Solution: def isSymmetric(self, root: TreeNode) -> bool: trace = [] def dfs(cur): if not cur: trace.append(None) else: if not (not cur.left and not cur.right): dfs(cur.left) trace.append(cur.v..
Min Stack - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. event를 flag로 해서 처리하는 풀이 from heapq import * class MinStack: def __init__(self): self.stack = [] self.min_stack = [] self.events = [0] * 30000 self.event_cnt = 0 def push(self, val: int) -> None: self.stack.append((val, ..
Intersection of Two Linked Lists - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 객체 ID값을 이용한 분기점 반환 class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: a_id_list = [] b_id_list = [] p_a, p_b = headA, headB while p_a: a_id_list.append(id..