530. Minimum Absolute Difference in BST
# 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 getMinimumDifference(self, root: Optional[TreeNode]) -> int:
# BST 이므로, inorder (좌, 현재, 우) 로 출력하면, 작->큰, 오름차순으로 값이 나옴
arr = []
def inorder(node):
if node.left:
inorder(node.left)
arr.append(node.val)
if node.right:
inorder(node.right)
inorder(root)
minima = float("inf")
for i in range(len(arr)-1):
minima = min(minima, arr[i+1]-arr[i])
return minima'LeetCode > Top Interview 150' 카테고리의 다른 글
| 137. Single Number II (0) | 2024.12.18 |
|---|---|
| 67. Add Binary (0) | 2024.12.17 |
| 103. Binary Tree Zigzag Level Order Traversal (0) | 2024.12.15 |
| 637. Average of Levels in Binary Tree (0) | 2024.12.14 |
| 222. Count Complete Tree Nodes (0) | 2024.12.14 |