leet code41 [LEET CODE] 208. Implement Trie (Prefix Tree) # Problem A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker. Implement the Trie class: Trie() Initializes the trie object. void insert(String word) Inserts the string word into the trie. boolean search(String word) Re.. 2021. 5. 26. [LEET CODE] 215. Kth Largest Element in an Array # Problem Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Constraints: 1 int: return sorted(nums, reverse=True)[k - 1] # 힙 ( heap ) 최소힙 - 부모가 항상 자식보다 작거나 같다 ( 좌우 트리는 정렬된 구조는 아님) 최대힙 - 부모가 항상 자식보다 크거나 같다 거의 완전한 트리인 특수한 트리 기반 자료구조 우선순위 큐 구현 2021. 5. 26. [LEET CODE] 105. Construct Binary Tree from Preorder and Inorder Traversal # Problem Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree. Constraints: 1 2021. 5. 24. [LEET CODE] 783. Minimum Distance Between BST Nodes # Problem Given the root of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree. Constraints: The number of nodes in the tree is in the range [2, 100]. 0 int: if root.left: self.minDiffInBST(root.left) self.result = min(self.result, root.val - self.prev) self.prev = root.val if root.right: self.minDiffInBST(root.right) return self.r.. 2021. 5. 24. [LEET CODE] 938. Range Sum of BST # Problem Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high]. Constraints: The number of nodes in the tree is in the range [1, 2 * 104]. 1 L: stack.append(node.left) if node.val < R: stack.append(node.right) if L L: stack.append(node.left) if node.val < R: stack.append(node.right) if L O.. 2021. 5. 24. [LEET CODE] 108. Convert Sorted Array to Binary Search Tree # Problem Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one. Constraints: 1 2021. 5. 19. 이전 1 2 3 4 ··· 7 다음 반응형