본문 바로가기

반응형

분류 전체보기

(66)
[LeetCode][Python3] 103. 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]]: queue = [] answer = [] level = [] if not root: return [] queue.append(root) while(len(queue)!=0): level.clear() for _ in range(0,len(queue)): node = queue...
[LeetCode][Python3] 104. Maximum Depth of Binary Tree # 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 maxDepth(self, root: TreeNode) -> int: def nowDepth(node,depth): if(node==None): return depth else: if(nowDepth(node.left,depth+1)>=nowDepth(node.right,depth+1)): return nowDepth(node.left,depth+1) else: return now..
[LeetCode][Python3] 100. Same Tree 풀이. # 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 isSameTree(self, p: TreeNode, q: TreeNode) -> bool: def isSame(p,q): if(p==None and q==None): return True elif(p==None or q == None): return False elif(p.val!=q.val): return False elif(p.val == q.val): return i..
[LeetCode][Python3] 102. Binary Tree Level Order Traversal 풀이 queue에 들어있는 node들은 같은 level입니다. 따라서 현재 queue의 node들을 꺼내 list에 넣어주고, node의 left와 right node들은 queue에 새로 넣어줍니다. # 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 levelOrder(self, root: TreeNode) -> List[List[int]]: queue = [] answer = [] level = [] if not root: r..
[LeetCode][Python3] 98. Validate Binary Search Tree # 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 isValidBST(self, root: TreeNode) -> bool: def isValid(node:TreeNode,low=-math.inf,high=math.inf): if not node: return True else: if(node.val=high): return False return isValid(node.left,low,node.val) and isValid(no..
[LeetCode][Python3] 17. Letter Combinations of a Phone Number 풀이 1 class Solution: def letterCombinations(self, digits: str) -> List[str]: if digits=="": return [] result=[] letter={'2':['a','b','c'],'3':['d','e','f'],'4':['g','h','i'],'5':['j','k','l'],'6':['m','n','o'],'7':['p','q','r','s'],'8':['t','u','v'],'9':['w','x','y','z']} def dfs(prefix: str,digits:str,length:int): if(length == len(digits)): result.append(prefix) return else: for i in letter[dig..
[LeetCode][Python3] 21. Merge Two Sorted Lists 풀이 1 class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: ary=[] while l1 or l2: if l1: ary.append(l1.val) l1=l1.next elif l2: ary.append(l2.val) l2=l2.next ary.sort() if len(ary)>0: l3=ListNode(ary[0],None) itr = l3 for i in ary[1:]: node = ListNode(i,None) itr.next=node itr=itr.next return l3 return None ary.sort() 을 통해 정렬을 할 수 있지만 이는 알고리즘 공부에 도움이 되지 않는다고 생각했다. 풀이 2..
[LeetCode][Python3] 20. Valid Parentheses class Solution: def isValid(self, s: str) -> bool: stack=[] if(len(s)%2==1): return False for c in s: if(c=="(" or c=="[" or c=="{"): stack.append(c) else: if len(stack)==0: return False popItem = stack.pop() if(popItem == '('): if(c==')'): continue else: return False elif(popItem == '['): if(c==']'): continue else: return False elif(popItem == '{'): if(c=='}'): continue else: return False if le..

반응형