본문 바로가기
반응형

분류 전체보기93

[DSA][Trees] 11. Validate Binary Search Tree LeetCode 98 11. Validate Binary Search TreeGiven the root of a binary tree, determine if it is a valid binary search tree (BST).A valid BST is defined as follows:- The left subtree of a node contains only nodes with keys less than the node's key.- The right subtree of a node contains only nodes with keys greater than the node's key.- Both the left and right subtrees must also be binary search tr.. 2025. 7. 20.
[DSA][Trees] 10. Count Good Nodes in Binary Tree LeetCode 1448 10. Count Good Nodes in Binary TreeGiven a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.Return the number of good nodes in the binary tree. [질문하기]- 경로상 최댓값과 현재 노드의 값이 같은 경우에도 good node인가요? - 노드 값은 양수인가요? NO [아이디어]- 재귀 함수를 사용하여 현재 노드와 maxVal을 비교하여 카운트한다. [풀이 1] Depth First Searchclass Solution: .. 2025. 7. 19.
[DSA][Trees] 09. Binary Tree Right Side View LeetCode 199 09. Binary Tree Right Side ViewGiven the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. [질문하기]- root가 null일 경우 빈 리스트를 반환하나요? [아이디어]- BFS 방식으로 트리를 순회하면서, 각 노드를 큐(deque)에 저장해 레벨 단위로 탐색한다. [풀이 1] Breadth First Searchclass Solution: def rightSideView(self, root: Optional[TreeNode]) -> .. 2025. 7. 18.
[DSA][Trees] 08. Binary Tree Level Order Traversal LeetCode 102 08. Binary Tree Level Order TraversalGiven the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level). [질문하기]- root가 null일 경우 빈 리스트를 반환하나요? [아이디어]- BFS 방식으로 트리를 순회하면서, 각 노드를 큐(deque)에 저장해 레벨 단위로 탐색한다. [풀이 1] Breadth First Searchclass Solution: def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: .. 2025. 7. 17.