본문 바로가기

Leetcode98

[LeetCode] Minimum Depth of Binary Tree 파이썬 (recursion) class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution: # @param root, a tree node # @return an integer def minDepth(self, root): if root is None: return 0 if root.left and root.right: return min(self.minDepth(root.left), self.minDepth(root.right)) + 1 else: return max(self.minDepth(root.left), self.minDepth(root.right)) + 1 max와 다른 점은 leaf node 까지의.. 2021. 4. 1.
[LeetCode] Balance Binary Tree python 파이썬 (recursion) # 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(object): def maxDepth(self, root): if root == None: return 0 else: return max( self.maxDepth(root.left), self.maxDepth(root.right)) + 1 def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ if root == No.. 2021. 4. 1.
[LeetCode] Convert Sorted Array to Binary Tree python 파이썬 (recursion, median) 솔루션을 보기 전에는 어떤식으로 풀어야 하는지 감이 안왔다. 솔루션을 보고 나니 Binary Search 개념을 사용해서 mid 지점만 root 가 되도록 하면 되는 것이다. class Solution: def sortedArrayToBST(self, nums: List[int]) -> TreeNode: def sortToBST(nums): if len(nums) == 0: return None mid = nums[len(nums) // 2] root = TreeNode(mid) root.left = sortToBST(nums[:len(nums) // 2]) root.right = sortToBST(nums[len(nums) // 2 + 1:]) return root return sortToBST(num.. 2021. 4. 1.
[LeetCode] Maximum Depth of Binary Tree 파이썬 (recursion) 40ms class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution: # @param root, a tree node # @return an integer def maxDepth(self, root): if root is None: return 0 else: return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 출처:github.com/jiapengwen/LeetCode/blob/master/Python/maximum-depth-of-binary-tree.py class Solution: def maxDepth(se.. 2021. 3. 31.
[LeetCode] Symmetric Tree 파이썬 (iterative, recursion) iterative solution (32ms) class Solution: # @param root, a tree node # @return a boolean def isSymmetric(self, root): if root is None: return True stack = [] stack.append(root.left) stack.append(root.right) while stack: p, q = stack.pop(), stack.pop() if p is None and q is None: continue if p is None or q is None or p.val != q.val: return False stack.append(p.left) stack.append(q.right) stack.ap.. 2021. 3. 31.
[LeetCode] Same Tree 파이썬 (TreeNode, recursion) class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution: # @param p, a tree node # @param q, a tree node # @return a boolean def isSameTree(self, p, q): if p is None and q is None: return True if p is not None and q is not None: return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) return False 출처: github.com.. 2021. 3. 31.
[LeetCode] Merge Sorted Array 파이썬 (modify in-place) class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-place instead. """ nums1[m:m+n] = nums2[:] nums1.sort() 2021. 3. 31.
[LeetCode] Remove Duplicates from Sorted List 파이썬 (Linked List, shallow copy, deepcopy) 하위 95% 솔루션 😱 64ms # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: total = [] while head: total.append(head.val) head = head.next total = list(sorted(set(total))) print(total) answer = None for i in range(len(total)): if i == 0: answer = ListNo.. 2021. 3. 31.
[LeetCode] Climbing Stairs 파이썬 (DP) 28ms class Solution: def climbStairs(self, n: int) -> int: if n == 1: return 1 elif n == 2: return 2 else: dp = [0]*(n+1) dp[1] = 1 dp[2] = 2 for i in range(3, n+1): dp[i] = dp[i-1] + dp[i-2] return dp[n] swap 사용 32 ms if n == 1: return 1 a, b = 1, 2 for i in range(2, n): a, b = b, a + b return b 2021. 3. 30.