LeetCode 938 Solution

LeetCode 938 Solution

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].

Example 1:

  • Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
  • Output: 32
  • Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.

Example 2:

  • Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
  • Output: 23
  • Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.

Binary Search Tree

A Binary Search Tree (BST) is a fundamental data structure that's essential in computer science for efficient data storage and retrieval. Here are the key points that highlight its importance and functionality:

  1. Definition and Structure:
    • A BST is a tree structure where each node can have up to two children, referred to as the left and right child.
    • The left child of a node always contains a value lesser than its parent node.
    • The right child always contains a value greater than its parent node.
    • This property must hold true for every subtree within the BST, not just the root.
  2. Efficient Search and Sorting:
    • BSTs allow for efficient searching of data, as they leverage the property of binary search. In an average case, operations like search, insert, and delete have a time complexity of $O(log n)$.
    • Inorder traversal of a BST yields the elements in a sorted order, making it useful for tasks that require ordered data.
  3. Imbalance Issues:
    • The efficiency of a BST can significantly deteriorate depending on the order of data insertion. For instance, inserting sorted data into a BST can lead to a skewed tree, resembling a linked list, where operations might take O(n) time.
    • Self-balancing binary search trees, such as AVL trees or Red-Black trees, are used to overcome this issue by maintaining a balanced height during insertions and deletions.
  4. Deletion Operation:
    • Deleting a node in a BST can be more complex, especially if the node has children.
    • If the node is a leaf (no children), it is simply removed.
    • For a node with one child, the node is replaced with its child.
    • For a node with two children, the node is generally replaced with its in-order predecessor (the maximum value in the left subtree) or successor (the minimum value in the right subtree), and then that predecessor or successor is deleted.
  5. Applications:
    • BSTs are utilized in various areas such as database management, file systems, and search engine optimization due to their efficiency in handling ordered data.

python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution:
    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:
        result = 0
        if root.val >= low and root.val <= high:
            result += root.val
        if root.left:
            result += self.rangeSumBST(root.left, low, high)
        if root.right:
            result += self.rangeSumBST(root.right, low, high)
        return result
  1. Recursive search
    1. Use rangeSumBST recursively
  2. Time complexity minimize
    1. $O(n)$ of time complexity

Read more

airflow 구성하고 vscode로 코딩하기

맥에서 했으면 훨씬 구성이 쉬웠겠지만, 그리고 poetry로 했으면 훨씬 쉬웠겠지만 워낙 규모가 있는 라이브러리이다 보니 과정이 어려워 다른 참조들을 보면서 따라했다. 기본적으로 poetry랑 쓰기 어려운 이유는 airflow 내부의 라이브러리에 따라 poetry가 버전을 참조하지 못해서 에러가 나는 경우가 존재한다고 한다. 또한 하나의 문제는 mac에서는 그냥 리눅스가 존재하지만 윈도우에서 하려면 윈도우용 linux인

[Json] dump vs dumps

json은 javascript object notation의 줄임말로 웹 어플리케이션에서 구조화된 데이터를 표현하기 위한 string 기반의 포맷이다. 서버에서 클라인트로 데이터를 전송하여 표현하거나, 그 반대로 클라이언트에서 서버로 보내는 경우들에 사용된다. javascript 객체 문법과 굉장히 유사하지만 워낙에 범용성이 넓게 설계되어 있어서 다른 언어들에도 많이 사용된다. 기본적으로 python 에는 json 이 내장 모듈이다. 바로 import json해주면