[LeetCode] 701. Insert into a Binary Search Tree, Medium
·
CodingTest/LeetCode
1. 문제BST와 정수 val이 주어질 때, BST가 유지되도록 val를 삽입한 뒤 반환하라.2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.righ..
[LeetCode] 700. Search in a Binary Search Tree, Easy
·
CodingTest/LeetCode
1. 문제BST와 정수 val이 주어질 때, val과 일치하는 노드를 반환하라.2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (rig..
[LeetCode] 173. Binary Search Tree Iterator, Medium
·
CodingTest/LeetCode
1. 문제이진 탐색 트리가 주어질 때, 중위 순회를 수행하여 요구사항을 만족하는 class를 구현하라.2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this..
[LeetCode] 98. Validate Binary Search Tree, Medium
·
CodingTest/LeetCode
1. 문제주어진 트리가 이진 탐색 트리를 만족하는 지 반환하라.2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===unde..
[LeetCode] 212. Word Search II, Hard
·
CodingTest/LeetCode
1. 문제문자열로 구성된 words 배열과, 문자로 이루어진 2차원 배열 board가 주어질 때 수평, 수직에 인접한 문자를 조합하여 만들 수 있는 단어 배열을 반환하라.각 단어에 같은 문자는 1번만 쓰일 수 있다.2. 해결class TrieNode { children: Map word: string; constructor() { this.children = new Map(); this.word = null; }}class Trie { root: TrieNode; constructor() { this.root = new TrieNode(); } insert(word:string) { let node = t..
[LeetCode] 211. Design Add and Search Words Data Structure, Medium
·
CodingTest/LeetCode
1. 문제요구 사항에 맞는 WordDictionary class를 구현하라.2. 해결class TrieNode { children: Map; isEnd: boolean constructor() { this.children = new Map(); this.isEnd = false; }}class WordDictionary { root: TrieNode; constructor() { this.root = new TrieNode(); } addWord(word: string): void { let node = this.root; for(const char of word) { ..
[LeetCode] 648. Replace Words, Medium
·
CodingTest/LeetCode
1. 문제어간으로 구성된 dictionary와 파생어로 구성된 sentence 문자열이 주어질 때, 파생어 요소 중 어간이 있는 요소는 어간으로 교체하여 반환하라.2. 해결class TrieNode { children:Map; root:string; constructor() { this.children = new Map(); this.root = ''; }}class Trie{ root:TrieNode; constructor() { this.root = new TrieNode(); } insert(word:string) { let node = this.root; let root = ''; ..
[LeetCode] 677. Map Sum Pairs, Medium
·
CodingTest/LeetCode
1. 문제요구사항에 맞는 MapSum class를 구현하라.2. 해결class TrieNode { children: Map; sum: Map; constructor() { this.children = new Map(); this.sum = new Map(); }}class MapSum { root: TrieNode; constructor() { this.root = new TrieNode(); } insert(key: string, val: number): void { let node = this.root; for(const char of key) { if(!..
[LeetCode] 208. Implement Trie (Prefix Tree), Medium
·
CodingTest/LeetCode
1. 문제문제 요구사항에 맞는 트라이를 구현하라.2. 해결class TrieNode { children:Map; isEnd: boolean constructor() { this.children = new Map(); this.isEnd = false; }}class Trie { root:TrieNode; constructor() { this.root = new TrieNode(); } insert(word: string): void { let node = this.root; for(const char of word) { if(!node.children.has(char..
[LeetCode] 347. Top K Frequent Elements, Medium
·
CodingTest/LeetCode
1. 문제정수 배열 nums와 정수 k가 주어질 때, 가장 많이 나오는 요소를 k개 담은 배열을 반환하라.2. 해결function topKFrequent(nums: number[], k: number): number[] { const map = new Map(); for(let i = 0; i b[1] - a[1]).slice(0,k).map(entry => entry[0])};map에 요소와 등장 회수를 key-value로 지정.해당 map에서 추출.