본문 바로가기

반응형

Algorithm

(20)
[LeetCode][Python3] 111. Minimum 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 minDepth(self, root: TreeNode) -> int: queue = [] if not root: return 0 depth = 1 queue.append(root) while(len(queue)!=0): for _ in range(0,len(queue)): node = queue.pop(0) if not node.left and not node.right: retu..
[LeetCode][Python3] 107. Binary Tree Level Order Traversal II # 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 levelOrderBottom(self, root: TreeNode) -> List[List[int]]: queue = [] answer = [] if not root: return [] queue.append(root) while(len(queue)!=0): floor=[] for _ in range(0,len(queue)): node = queue.pop(0) if node.l..
[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..

반응형