❒ Description
날짜 | 2024.09.25 (수) |
레벨 | Easy |
제목 | Minimum Distance Between BST Nodes |
링크 | https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/ |
자료구조 | 그래프 - BST |
시간 복잡도 | O(n) |
[LeetCode#1038] Binary Search Tree to Greater Sum Tree 문제와 거의 유사한 문제이다.
❒ Solution
private int prev = Integer.MIN_VALUE + 10000;
private int minDiff = Integer.MAX_VALUE;
public int minDiffInBST(TreeNode root) {
if (root != null) {
minDiffInBST(root.left);
minDiff = Math.min(minDiff, root.val - prev);
prev = root.val;
minDiffInBST(root.right);
}
return minDiff;
}
'Algorithm > 문제풀이' 카테고리의 다른 글
[LeetCode#148] Sort List (0) | 2024.09.27 |
---|---|
[LeetCode#105]Construct BT from Preorder and Inorder Traversal (0) | 2024.09.26 |
[LeetCode#938] Range Sum of BST (0) | 2024.09.25 |
[LeetCode#1038] Binary Search Tree to Greater Sum Tree (0) | 2024.09.25 |
[LeetCode#108] Convert Sorted Array to Binary Search Tree (0) | 2024.09.25 |