๋ฌธ์ ํ์

k
๋ฒ์งธ๋ก ์์ ๊ฐ์ ์ถ๋ ฅํด์ผํ๋ค.
ํ์ด
1๏ธโฃ ์ค์ ์ํ๋ฅผ ์ด์ฉํ ํ์ด
๐ก ๋ ์ค๋ฅธ Idea
ํธ๋ฆฌ ๊ตฌ์กฐ์์ ์ค์ ์ํ๋ฅผ ํ๋ฉด ์ค๋ฆ์ฐจ์ ์์๋ก ๋ ธ๋ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ ์ ์๋ค.
์ด๋ฌํ ์๋ฆฌ๋ฅผ ์ ์ฉํ์ฌ k๋ฒ์งธ ๋ ธ๋์ ์ ๊ทผํ์ ๋, ๊ฐ์ ์ ์ฅํ๋๋ก ๊ตฌํํ๋ฉด ๋๊ฒ ๋ค๊ณ ํ๋จํ์๋ค.
class Solution {
int ans = 0;
int idx = 1;
public int kthSmallest(TreeNode root, int k) {
inOrder(root, k);
return ans;
}
private void inOrder(TreeNode node, int k) {
if (node == null) {
return;
}
inOrder(node.left, k);
if (idx == k) {
ans = node.val;
}
idx++;
inOrder(node.right, k);
}
}
๊ฒฐ๊ณผ

๐ ํ๊ณ
์ด์ ์ ํด๊ฒฐํ๋,
Minimum Absolute Difference in BST - LeetCode
Can you solve this real interview question? Minimum Absolute Difference in BST - Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree. Example 1: [https://assets.l
leetcode.com
์ด ๋ฌธ์ ์ ํ์ด ๋ฐฉ์์ด ๊ฑฐ์ ๋์ผํด์ ์ฝ๊ฒ ํด๊ฒฐํ ์ ์์๋ค.