• 문제 링크
Minimum Distance Between BST Nodes - LeetCode
Can you solve this real interview question? Minimum Distance Between BST Nodes - 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: [https://assets.leetcode.c
leetcode.com
• 풀이 코드
public class Solution {
private int min = Integer.MAX_VALUE, prev = -1;
public int minDiffInBST(TreeNode root) {
inorder(root);
return min;
}
private void inorder(TreeNode node) {
if (node == null) return;
inorder(node.left);
if (prev != -1) min = Math.min(min, node.val - prev);
prev = node.val;
inorder(node.right);
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 796. Rotate String - Java (0) | 2026.10.15 |
|---|---|
| [LeetCode] 771. Jewels and Stones - Java (0) | 2026.10.13 |
| [LeetCode] 766. Toeplitz Matrix - Java (0) | 2026.10.12 |
| [LeetCode] 762. Prime Number of Set Bits in Binary Representation - Java (0) | 2026.10.11 |
| [LeetCode] 748. Shortest Completing Word - Java (0) | 2026.10.10 |
댓글