KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • Level 2
  • Level 1
  • 그래프
  • 백준
  • tree
  • C
  • js
  • Level1
  • 정렬
  • Level 0
  • string
  • 문자열
  • C++
  • 제코베 JS 100제
  • 수학
  • programmers
  • codeup
  • recursion
  • 다이나믹 프로그래밍
  • LeetCode

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Binary Search Tree / 230번 / Kth Smallest Element in a BST / JS

2023. 4. 29. 18:00

< 문제 바로가기 >

 

Kth Smallest Element in a BST - LeetCode

Can you solve this real interview question? Kth Smallest Element in a BST - Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.   Example 1: [https://assets.leetco

leetcode.com

 

< 문제 간단설명 >

BST(Binary Search Tree) 에서 K번째로 작은 노드의 값을 반환하는 문제이다.

 

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {number}
 */
var kthSmallest = function (root, k) {
  let nodeList = [];

  // 전위 순회 (DFS)
  const traverse = (node) => {
    if (node === null) {
      return;
    }

    // 왼쪽 먼저 끝까지 순회하면서 nodeList에 넣고,
    traverse(node.left);
    nodeList.push(node.val);
    // 오른쪽 순회
    traverse(node.right);
  };

  traverse(root);

  // 전위 순회하면서 이미 정렬되어 있으므로 문제에 맞는 순서에 해당하는 값 반환
  let result = nodeList.at(k - 1);
  return result;
};
저작자표시

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Graph / 994번 / Rotting Oranges / JS  (0) 2023.04.29
LeetCode / Binary Search Tree / 173번 / Binary Search Tree Iterator / JS  (0) 2023.04.29
LeetCode / Binary Search Tree / 108번 / Convert Sorted Array to Binary Search Tree / JS  (0) 2023.04.29
LeetCode / Binary Search / 33번 / Search in Rotated Sorted Array / JS  (0) 2023.04.28
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Graph / 994번 / Rotting Oranges / JS
    • LeetCode / Binary Search Tree / 173번 / Binary Search Tree Iterator / JS
    • LeetCode / Binary Search Tree / 108번 / Convert Sorted Array to Binary Search Tree / JS
    • LeetCode / Binary Search / 33번 / Search in Rotated Sorted Array / JS
    KimMinJun
    KimMinJun

    티스토리툴바