본문 바로가기

Algorithm/LEET CODE ( 파이썬 알고리즘 인터뷰)56

[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] 1038. Binary Search Tree to Greater Sum Tree # Problem Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. As a reminder, a binary search tree is a tree that satisfies these constraints: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of.. 2021. 5. 24.
반응형