본문 바로가기

전체 글

(31)
7/20 코테 일지 (Helper 함수) 1. Helper 함수https://leetcode.com/problems/path-sum/description/Class 내 정의된 nested 함수를 helper 함수라고 부른다. 이 helper 함수는 클래스 내에서만 사용되는 보조적인 역할을 하며, 주로 클래스의 메서드들이 수행해야 하는 반복적인 작업을 분리하여 코드의 가독성을 높이고, 중복을 줄이기 위해 사용된다.밑의 코드는 내가 짠 코드이며 helper 함수를 사용하지 않는다.class Solution(object): def hasPathSum(self, root, targetSum,sum =0): if not root: return False sum += root.val if no..
NeetCode 22번 알고리즘 Tree Maze AlgorithmQ: Determine if a path exists from the root of the tree to a leaf node. It may not contain any zeroes. Codeclass TreeNode: def __init__(self, val): self.val = val self.left = None self.right = Nonedef leafPath(root, path): if not root or root.val == 0: return False path.append(root.val) if not root.left and not root.right: return True if..
7/10 코테 일지 (exec() 함수) exec() 함수#1991 트리 순회 트리 순회 문제에서 input이 알파벳으로 들어오면 해당 알파벳을 이름으로 가지는 새로운 TreeNode()를 생성하여야 했다. 어떻게 하면 가능할까 하였는데 exec() 함수를 사용하면 가능하였다. import sysn = int(sys.stdin.readline().rstrip())root_set = set()for _ in range(n): input = sys.stdin.readline().rstrip().split(' ') for node in input : if node not in root_set and node != '.': root_set.add(node) exec(f"{node} = TreeNode('{node}')") ..