우규이인우윀
Eager To Learn 🌌
우규이인우윀
전체 방문자
였늘
μ–΄μ œ

λΈ”λ‘œκ·Έ 메뉴

  • 🏑 ν™ˆ
  • πŸš€ κΉƒν—ˆλΈŒ
  • β›… νƒœκ·Έ ν΄λΌμš°λ“œ
  • λΆ„λ₯˜ 전체보기 (217)
    • πŸ‘¨πŸ»‍πŸ’» PS (170)
      • JAVA (82)
      • MYSQL (1)
      • Docker (2)
      • PYTHON (24)
      • LeetCode 150 (39)
      • Algorithm 기법 (1)
      • 바킹독 (21)
    • λΈ”λ‘œκ·Έ 이사 (0)
    • Error (1)
    • CS (15)
      • DataBase (2)
      • OS (7)
      • Network (1)
      • Spring (1)
      • 자료ꡬ쑰 (3)
      • Java (1)
    • Learned (7)
      • Spring (7)
    • κ°œλ°œμ„œμ  (15)
      • 가상 λ©΄μ ‘ μ‚¬λ‘€λ‘œ λ°°μš°λŠ” λŒ€κ·œλͺ¨ μ‹œμŠ€ν…œ 섀계 기초 (1)
      • 였브젝트 - 쑰영호 (7)
      • μΉœμ ˆν•œ SQL νŠœλ‹ (7)
    • 회고 (2)
hELLO Β· Designed By μ •μƒμš°.
우규이인우윀

Eager To Learn 🌌

πŸ‘¨πŸ»‍πŸ’» PS/LeetCode 150

[Java] 230. Kth Smallest Element in a BST

2023. 9. 6. 10:44

문제 νŒŒμ•…

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

 

이 λ¬Έμ œμ™€ 풀이 방식이 거의 λ™μΌν•΄μ„œ μ‰½κ²Œ ν•΄κ²°ν•  수 μžˆμ—ˆλ‹€.

 

 

    'πŸ‘¨πŸ»‍πŸ’» PS/LeetCode 150' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
    • [Java] 637. Average of Levels in Binary Tree
    • [Java] 199. Binary Tree Right Side View
    • [Java] 530. Minimum Absolute Difference in BST
    • [Java] 153. Find Minimum in Rotated Sorted Array
    우규이인우윀
    우규이인우윀
    개발자 κΏˆλ‚˜λ¬΄

    ν‹°μŠ€ν† λ¦¬νˆ΄λ°”