783. Minimum Distance Between BST Nodes
Description
Given the root
of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree.
Example 1:
- Input: root = [4,2,6,1,3]
- Output: 1
Example 2:
- Input: root = [1,0,48,null,null,12,49]
- Output: 1
Constraints:
- The number of nodes in the tree is in the range
[2, 100]
. - 0 <= Node.val <= 105
Note: This question is the same as 530: https://leetcode.com/problems/minimum-absolute-difference-in-bst/
Submitted Code
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def minDiffInBST(self, root):
"""
:type root: Optional[TreeNode]
:rtype: int
"""
self.prev = None
self.min_diff = float('inf')
def inorder(node):
if node:
inorder(node.left)
if self.prev is not None:
self.min_diff = min(self.min_diff, (node.val - self.prev))
self.prev = node.val
inorder(node.right)
inorder(root)
return self.min_diff
Runtime: 0 ms | Beats 100.00%
Memory: 12.45 MB | Beats 96.41%
530번과 동일한 문제로, 중위 순회로 정렬된 결과에서 앞뒤 노드끼리의 차이를 비교한다.
Other Solutions
1st
class Solution(object):
pre = -float('inf')
res = float('inf')
def minDiffInBST(self, root):
if root.left:
self.minDiffInBST(root.left)
self.res = min(self.res, root.val - self.pre)
self.pre = root.val
if root.right:
self.minDiffInBST(root.right)
return self.res
time complexity: 𝑂(𝑛)
space complexity: 𝑂(ℎ)
이전에 방문한 노드의 값을 저장하는 pre
의 값을 None이 아니라 음의 무한대로 설정하면 더 간단하게 코드를 짤 수 있다.