https://leetcode.com/problems/minimum-absolute-difference-in-bst/
Minimum Absolute Difference in BST - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제: 트리의 노드의 중에서 두 노드의 차가 제일 작은 값을 구하는 문제
풀이 코드:
class Solution:
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
tree = []
def getVal(root: Optional[TreeNode]):
tree.append(root.val)
if root.left != None:
getVal(root.left)
if root.right != None:
getVal(root.right)
getVal(root)
tree = sorted(tree)
return min(tree[i + 1] - tree[i] for i in range(len(tree) - 1))
'알고리즘 > Leetcode' 카테고리의 다른 글
[Leetcode / python] 777번,2337번 공통 풀이 (0) | 2022.11.17 |
---|---|
[Leetcode / python] 2079. Watering Plants (0) | 2022.11.16 |
[Leetcode / python] 2. Add Two Numbers (0) | 2022.11.15 |
[Leetcode / python] 1652. Defuse the Bomb (0) | 2022.11.14 |
[Leetcode / python] 1207. Unique Number of Occurrences (0) | 2022.11.14 |