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)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun
PS/LeetCode

LeetCode / Binary Search Tree / 173번 / Binary Search Tree Iterator / JS

PS/LeetCode

LeetCode / Binary Search Tree / 173번 / Binary Search Tree Iterator / JS

2023. 4. 29. 18:02

< 문제 바로가기 >

 

Binary Search Tree Iterator - LeetCode

Can you solve this real interview question? Binary Search Tree Iterator - Implement the BSTIterator class that represents an iterator over the in-order traversal [https://en.wikipedia.org/wiki/Tree_traversal#In-order_(LNR)] of a binary search tree (BST): *

leetcode.com

 

< 문제 간단설명 >

이진 탐색 트리를 전위순회를 하였을 때, 다음 노드를 반환하는 next()와 다음 노드가 존재하는지 반환하는 hasNext()를 직접 구현하는 문제이다.

 

/**
 * 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
 */
var BSTIterator = function (root) {
  this.queue = [];
  this.index = 0;
  this.node = root;

  const traverse = (node) => {
    if (node === null) {
      return null;
    }

    // 전위 순회
    traverse(node.left);
    this.queue.push(node);
    traverse(node.right);
  };

  traverse(root);
};

/**
 * @return {number}
 */
BSTIterator.prototype.next = function () {
  // node에 현재 index의 다음 index가 가리키는 값을 저장하고,
  // index += 1
  this.node = this.queue[this.index++];
  // 현재 노드의 값을 반환
  return this.node.val;
};

/**
 * @return {boolean}
 */
BSTIterator.prototype.hasNext = function () {
  // 현재 가리키는 인덱스가 맨 끝이 아니라면 true
  return this.index !== this.queue.length;
};

/**
 * Your BSTIterator object will be instantiated and called as such:
 * var obj = new BSTIterator(root)
 * var param_1 = obj.next()
 * var param_2 = obj.hasNext()
 */
저작자표시 (새창열림)

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

LeetCode / Graph / 417번 / Pacific Atlantic Water Flow / JS  (0) 2023.04.29
LeetCode / Graph / 994번 / Rotting Oranges / JS  (0) 2023.04.29
LeetCode / Binary Search Tree / 230번 / Kth Smallest Element in a BST / JS  (0) 2023.04.29
LeetCode / Binary Search Tree / 108번 / Convert Sorted Array to Binary Search Tree / JS  (0) 2023.04.29
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Graph / 417번 / Pacific Atlantic Water Flow / JS
    • LeetCode / Graph / 994번 / Rotting Oranges / JS
    • LeetCode / Binary Search Tree / 230번 / Kth Smallest Element in a BST / JS
    • LeetCode / Binary Search Tree / 108번 / Convert Sorted Array to Binary Search Tree / JS
    KimMinJun
    KimMinJun

    티스토리툴바

    단축키

    내 블로그

    내 블로그 - 관리자 홈 전환
    Q
    Q
    새 글 쓰기
    W
    W

    블로그 게시글

    글 수정 (권한 있는 경우)
    E
    E
    댓글 영역으로 이동
    C
    C

    모든 영역

    이 페이지의 URL 복사
    S
    S
    맨 위로 이동
    T
    T
    티스토리 홈 이동
    H
    H
    단축키 안내
    Shift + /
    ⇧ + /

    * 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.