Kth Smallest Element in a BST - Leetcode 230 - Trees (Python)

Поділитися
Вставка
  • Опубліковано 5 січ 2025

КОМЕНТАРІ • 22

  • @GregHogg
    @GregHogg  5 місяців тому +1

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @kavan3
    @kavan3 2 місяці тому +3

    I'd recommend to make "count" and "ans" member variables and access them using self.count and self.ans instead of that list hack. It's more clear on what is going on and is easier to explain to an interviewer

  • @Matem-sc1ic
    @Matem-sc1ic 6 місяців тому +3

    Such an amazing content ❤
    Can you make unique binary search tree problem next

    • @GregHogg
      @GregHogg  6 місяців тому +1

      Thank you so much! Maybe I'll see :)

  • @harshnj
    @harshnj 6 місяців тому +5

    Why use count = [k] and ans = [0] instead of just k or 0. Why use a single element list there?

    • @ibrahimsyahz
      @ibrahimsyahz 6 місяців тому +1

      Same question

    • @Infinitely16
      @Infinitely16 3 місяці тому +2

      In Python, if you just used variables, then when you update the values in any particular recursive call, that variable technically isn't available and you will get an error because you defined the variable outside of the the scope of that local recursive call. If you put the value as an element of an array, it has a global memory address that is accessible regardless of which recursive call you are in. Greg goes over this in his Diameter of Binary Tree video: ua-cam.com/video/6lJZ_xj1mEo/v-deo.html

    • @harshnj
      @harshnj 3 місяці тому

      @@Infinitely16 Thanks very much for the detailed explanation! I will go over the video

    • @Philgob
      @Philgob 3 місяці тому +4

      just use self.count and self.ans not this odd workaround, that'd be a weird thing to explain in an interview

    • @harshnj
      @harshnj 3 місяці тому

      @@Philgob right! self variables have global access too

  • @NewTypeVietnam
    @NewTypeVietnam 6 місяців тому +1

    Amazing content. Can you make video on 105. Construct Binary Tree from Preorder and Inorder Traversal? Tks

  • @AnjanaOuseph
    @AnjanaOuseph 16 днів тому

    The time complexity is O(N) or is it O(H+K) ?

  • @codinggigs8367
    @codinggigs8367 3 місяці тому

    Here is the solution I come up with after watching your explanation: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
    result = []
    def dfs(root):
    if not root:return None
    dfs(root.left)
    result.append(root.val)
    dfs(root.right)
    dfs(root)
    return result[k-1]

  • @splendidbeaver2027
    @splendidbeaver2027 Місяць тому

    why not using nonlocal ?

  • @gaurangdeka
    @gaurangdeka 6 місяців тому

    This should be more simpler to understand:
    class Solution(object):
    def kthSmallest(self, root, k):
    res=[]
    def dfs(root):
    if not root:
    return
    dfs(root.left)
    res.append(root.val)
    dfs(root.right)
    dfs(root)
    return res[k-1]

    • @christianjt7018
      @christianjt7018 5 місяців тому

      @@GarouNguyen because the list is 0-indexed, so if he wants the element 5 of the list he needs to use the element: res[5-1]

    • @Philgob
      @Philgob 3 місяці тому

      this forces you to visit the whole tree, it's accepted on leetcode but worse even tho complexity is the same (imagine massive tree and k = 1000000)

  • @mkhile1589
    @mkhile1589 6 місяців тому

    Heap sort