KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (487)
    • 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 (432)
      • 백준 (187)
      • Programmers (105)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Binary Search Tree / 108번 / Convert Sorted Array to Binary Search Tree / JS

2023. 4. 29. 17:58

< 문제 바로가기 >

 

Convert Sorted Array to Binary Search Tree - LeetCode

Can you solve this real interview question? Convert Sorted Array to Binary Search Tree - Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.   Example 1: [https://assets.leetcod

leetcode.com

 

< 문제 간단설명 >

input으로 들어온 정렬된 배열을 이진 탐색 트리로 변환하는 문제이다.

 

/**
 * 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 {number[]} nums
 * @return {TreeNode}
 */
var sortedArrayToBST = function (nums) {
  if (nums.length === 0) {
    return null;
  }

  let mid = Math.floor(nums.length / 2);
  let root = new TreeNode(nums[mid], null, null);

  // 가운데는 root node 이기 때문에 가운데를 빼고 왼쪽, 오른쪽으로 나눠줌
  let left = nums.slice(0, mid);
  let right = nums.slice(mid + 1);

  root.left = sortedArrayToBST(left);
  root.right = sortedArrayToBST(right);

  return root;
};
저작자표시 (새창열림)

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

LeetCode / Binary Search Tree / 173번 / Binary Search Tree Iterator / JS  (0) 2023.04.29
LeetCode / Binary Search Tree / 230번 / Kth Smallest Element in a BST / JS  (0) 2023.04.29
LeetCode / Binary Search / 33번 / Search in Rotated Sorted Array / JS  (0) 2023.04.28
LeetCode / Binary Search / 74번 / Search a 2D Matrix / JS  (0) 2023.04.28
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Binary Search Tree / 173번 / Binary Search Tree Iterator / JS
    • LeetCode / Binary Search Tree / 230번 / Kth Smallest Element in a BST / JS
    • LeetCode / Binary Search / 33번 / Search in Rotated Sorted Array / JS
    • LeetCode / Binary Search / 74번 / Search a 2D Matrix / JS
    KimMinJun
    KimMinJun

    티스토리툴바