본문 바로가기

트리14

[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] 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.
[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.
❗️[LEET CODE] 310. Minimum Height Trees # Problem A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree. Given a tree of n nodes labelled from 0 to n - 1, and an array of n - 1 edges where edges[i] = [ai, bi] indicates that there is an undirected edge between the two nodes ai and bi in the tree, you can choose any node of the tree .. 2021. 4. 20.
[LEET CODE] 297. Serialize and Deserialize Binary Tree # Problem Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserial.. 2021. 4. 20.
[LEET CODE] 110. Balanced Binary Tree # Problem Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: Constraints: The number of nodes in the tree is in the range [0, 5000]. -104 1: return False return a and b # 서브 트리도 모두 균형인지 확인 # Solution 1 - 재귀 구조로 높이 차이 계산 class Solution: def isBalanced(self, root: TreeNode) -> bool: def check(root): if not root: return 0 left = c.. 2021. 4. 20.
반응형