본문 바로가기

Algorithm/LEET CODE ( 파이썬 알고리즘 인터뷰)56

[LEET CODE] 108. Convert Sorted Array to Binary Search Tree # Problem Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one. Constraints: 1 2021. 5. 19.
❗️[LEET CODE] 310. Minimum Height Trees # Problem A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree. Given a tree of n nodes labelled from 0 to n - 1, and an array of n - 1 edges where edges[i] = [ai, bi] indicates that there is an undirected edge between the two nodes ai and bi in the tree, you can choose any node of the tree .. 2021. 4. 20.
[LEET CODE] 297. Serialize and Deserialize Binary Tree # Problem Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserial.. 2021. 4. 20.
[LEET CODE] 110. Balanced Binary Tree # Problem Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: Constraints: The number of nodes in the tree is in the range [0, 5000]. -104 1: return False return a and b # 서브 트리도 모두 균형인지 확인 # Solution 1 - 재귀 구조로 높이 차이 계산 class Solution: def isBalanced(self, root: TreeNode) -> bool: def check(root): if not root: return 0 left = c.. 2021. 4. 20.
[LEET CODE] 617. Merge Two Binary Trees # Problem You are given two binary trees root1 and root2. Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the no.. 2021. 4. 20.
[LEET CODE] 226. Invert Binary Tree # Problem Given the root of a binary tree, invert the tree, and return its root. Constraints: The number of nodes in the tree is in the range [0, 100]. -100 TreeNode: if root: root.left, root.right = \ self.invertTree(root.right), self.invertTree(root.left) return root return None return None은 생략가능 ( 파이썬은 자동으로 None 리턴 ) # Solution 2 - 반복구조로 BFS class Solution: def invertTree(self, root: TreeNode.. 2021. 4. 20.
반응형