Delete Nodes and Return Forest

Sdílet
Vložit
  • čas přidán 23. 07. 2024
  • For business inquiries email partnerships@k2.codes Discord: bit.ly/K2-discord
  • Věda a technologie

Komentáře • 78

  • @KevinNaughtonJr
    @KevinNaughtonJr  Před 4 lety +1

    Looking to make a Q&A video soon! Follow me on Instagram and shoot me a DM with your question if you're interested in participating!!!
    instagram.com/kevinnaughtonjr/

  • @klsajdklajldkadad
    @klsajdklajldkadad Před 4 lety +30

    Just wondering.. how many attempts or how much time did it take you to initially solve these problems? I am still getting up to speed myself for some interviews, and it takes me a long time to figure out some of these. Love the videos and explanations.

  • @sunnilabeouf
    @sunnilabeouf Před 4 lety +1

    These videos are incredibly helpful, really appreciate the way you walk through the problem instead of just diving into the code.

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety

      Abdulrahman Tolba anytime!

    • @sunnilabeouf
      @sunnilabeouf Před 4 lety

      @@KevinNaughtonJr If you have the time, could you go over the Maximal Square (leetcode.com/problems/maximal-square/ ) problem? I've read the leetcode article for the solution, but it's a bit confusing imo

  • @SelftaughtSoftwareEngineer

    The best explanation I could find on CZcams about this problem. Thank you!

  • @safakhan8177
    @safakhan8177 Před 3 lety +1

    Thank you Kevin. This is very neat! I was able to solve the question in Python after watching your explanation and demo. Thanks again!

  • @navster8
    @navster8 Před 4 lety +8

    Super helpful video, I attempted this problem before watching and kind of pidgeon holed myself by checking vals on the way down as opposed to bottom up. Any advice on how to have the intuition when something will be bottom up/top down with regards to tree recursion (or is it safe to assume we'll probably always want a bottom up approach?).

  • @candicerusser9095
    @candicerusser9095 Před 4 lety +18

    Hey Kevin, could you do questions on dynamic programming. Thank you.

  • @JM_utube
    @JM_utube Před 4 lety +1

    good way to teach the concept of bottom up tree processing with recursive DFS. haven't come across many of these problems yet. thanks, Kevin

  • @ankitagarwal4914
    @ankitagarwal4914 Před 4 lety +3

    Amazing! I forgot to like and comment, so i came back to do it... Your content is full of learning and each line you speak weighs..

  • @considerphi
    @considerphi Před 4 lety

    Hey thanks for the work on these videos. Do you have a list of all of the popular questions?

  • @stanislavmozolevskiy8346
    @stanislavmozolevskiy8346 Před 8 měsíci

    I like the simple explanation. Good job.

  • @johanndiedrick4354
    @johanndiedrick4354 Před 4 lety +1

    you are the big homie. thanks for another great vid.

  • @mikasaackerman2694
    @mikasaackerman2694 Před 4 lety +1

    Nice Explanation! So I'm new to this, could you tell me if this bottom up-approach is what is known as back-tracking?

  • @sergten
    @sergten Před 3 lety

    Would it be more consistent in the main function instead of checking for if (!toDelete.contains(root.val)) {...} - check the return value of removeNodes(), and if it's NULL - add the root? Of course that'll fail if we got root==null as an argument, so an additional check for that will be needed. But it'll be needed in the current implementation as well.

  • @pradeepkumar-qo8lu
    @pradeepkumar-qo8lu Před 4 lety

    Is this similar to deleting nodes in a heap (max or min) or what makes this different from deleting nodes from heap ?

  • @pjac744
    @pjac744 Před 4 lety

    Thanks for the amazing explanation!

  • @brianevans4975
    @brianevans4975 Před 2 lety +1

    Did not expect that drop at the beginning lmao

  • @SR-we1vl
    @SR-we1vl Před 3 lety +1

    Hey Kevin, I was wondering how is it making List inside the ArrayList?

  • @pramodsharma4508
    @pramodsharma4508 Před 4 lety +1

    Amazing explaination...your videos are really very helpful...

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 3 lety

      Thank you! If you find my explanations helpful be sure to sign up for the interviewing service I created thedailybyte.dev/?ref=kevin I'd recommend joining the annual tier if you can!

  • @ianchui
    @ianchui Před 4 lety +1

    Great video! Thanks! (:

  • @rosiebahrani9895
    @rosiebahrani9895 Před 3 lety +1

    For the top-bottom approach, can't you just "save what your left and right sub trees are before removing the root? This way, you won't lose the reference to them... could you explain why this doesn't work?

  • @waterislife9
    @waterislife9 Před 4 lety +1

    bottom-up approach hint really clicked. thanks kevin!

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety

      dm anytime! If you need help with interview prep be sure to subscribe to The Daily Byte! thedailybyte.dev/

  • @shubhendraraj114
    @shubhendraraj114 Před 4 lety

    Hi Kevin, I just help your help as I got a rejection in the interview. Can you please tell me how to reach you out ?

  • @gurdoman
    @gurdoman Před 4 lety +1

    You deserve to be rich with CZcams money, hopefully you'll find sponsors and a boom in channel viewers.
    You are a better teacher than most of my teacher in college

  • @phanidavuluru
    @phanidavuluru Před 4 lety +2

    Cool video,as always !!
    Bruh,find a way to popularize your channel cause it has good content and is of great help to computer science students like me.

  • @cnaught0802
    @cnaught0802 Před 4 lety

    Killing it!

  • @algorithmimplementer415

    Google questions are so ambiguous and tricky, though solutions can be so simple if understood properly.

  • @thecheekychinaman6713
    @thecheekychinaman6713 Před 4 lety

    Hi Kevin, how come we return null (above the return root) instead of appending it to remain? I tried it out, doesnt work - but I can't explain it - we arent ever calling append on the null value

    • @JM_utube
      @JM_utube Před 4 lety

      you know, you can also do root = removeNodes(root ...); then check (if root != null) {add to remaining}

  • @ijaz2020
    @ijaz2020 Před 4 lety

    Hi Kevin, I think you have not posted any videos regarding binary search an array type of questions. Request to upload some.

  • @saravanansarangan7035
    @saravanansarangan7035 Před 4 lety +1

    Thanks Kevin

  • @kirant5548
    @kirant5548 Před 2 lety

    good one.

  • @santhoshmedide5279
    @santhoshmedide5279 Před 3 lety

    Amazing solution!

  • @alirahal5854
    @alirahal5854 Před 2 lety

    Since we are adding only left or right node to the remaining array , how will the sub array be created it the final result should be an array of nodes or roots not an array of sub arrays. I would like to know how the sub arrays were
    created

  • @alexeydeynega7603
    @alexeydeynega7603 Před 4 lety

    Hi Kevin. I like your background music, could you please tell who is playing?

  • @vibhav3811
    @vibhav3811 Před 4 lety +1

    great kevin

  • @emilyhuang2759
    @emilyhuang2759 Před 2 lety

    Could a BFS also work?

  • @backendengineering007
    @backendengineering007 Před 4 lety +1

    The return type for the removeNode(...) method should be TreeNode and not void. I am not sure how it compiled and executed successfully for you?? Or did I miss something?

  • @ayusheeagarwal955
    @ayusheeagarwal955 Před rokem

    Do we really need to delete the node? We just have to return remaining nodes from tree if given nodes were deleted.

  • @HenggaoCai
    @HenggaoCai Před 3 lety

    The return type of removeNode should be TreeNode.

  • @sharkk2979
    @sharkk2979 Před 3 lety

    Ty,kevin

  • @jak0495
    @jak0495 Před 4 lety +2

    You should return to the forest.

  • @mohithadiyal6083
    @mohithadiyal6083 Před 2 lety

    Why you never try to run code and just submit it directly after writing

  • @AmolGautam
    @AmolGautam Před 2 měsíci

    thanks

  • @ShaliniNegi24
    @ShaliniNegi24 Před 4 lety

    I did the same in the first attempt!

  • @qinyunli152
    @qinyunli152 Před 11 měsíci

    void , you cannot return type

  • @algorithmimplementer415
    @algorithmimplementer415 Před 4 lety +3

    You Write code so beautifully.

  • @guruvigneshnagaraj8501

    Why is the space complexity O(N)? Shouldn't it be more?

  • @DrJavyMartinez
    @DrJavyMartinez Před 2 lety +1

    You wayyyyyy overcomplicated this.

  • @sankethb.k642
    @sankethb.k642 Před 4 lety +2

    Thank you,
    Please upload the solution for house robber 3 .

    • @tanayakarmakar2407
      @tanayakarmakar2407 Před 4 lety +1

      class Solution {
      public int findMax(TreeNode root, boolean rob) {
      if(root == null)
      return 0;
      if(rob)
      return root.val + findMax(root.left, !rob) + findMax(root.right, !rob);
      else
      return Integer.max(findMax(root.left, !rob), findMax(root.left, rob)) + Integer.max(findMax(root.right, !rob), findMax(root.right, rob));
      }
      public int rob(TreeNode root) {
      int option1 = findMax(root, true);
      int option2 = findMax(root, false);
      return Integer.max(option1, option2);
      }
      }

    • @uditagrawal6603
      @uditagrawal6603 Před 3 lety +1

      class Solution {
      Map map = new HashMap();
      public int rob(TreeNode root) {
      if(root == null) return 0;
      if(map.get(root) != null) return map.get(root);
      int total = 0;
      if(root.left != null){
      total += rob(root.left.left) + rob(root.left.right);
      }
      if(root.right != null){
      total += rob(root.right.left) + rob(root.right.right);
      }
      int ans = Math.max(root.val + total, rob(root.left) + rob(root.right));
      map.put(root, ans);
      return ans;
      }
      }

  • @sharmanihal99
    @sharmanihal99 Před 20 dny

    Just one word: wow

  • @arindrajitseal4577
    @arindrajitseal4577 Před 4 lety

    You are pretty awesome

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety

      Arindrajit Seal thanks! If you need help with interview prep be sure to subscribe to the interviewing service I created, The Daily Byte! I recommend joining the annual plan thedailybyte.dev/

    • @arindrajitseal4577
      @arindrajitseal4577 Před 4 lety

      @@KevinNaughtonJr okay will look into it

  • @faraz7409
    @faraz7409 Před 4 lety +2

    thanks kev

  • @ashuadkar
    @ashuadkar Před 3 lety +1

    DFS is a more generic term. I would call it post order traversal

  • @john_rambo_27098
    @john_rambo_27098 Před 3 lety

    Why do you directly hit the "Submit" button, why not try "Run Code" first :P Jokes, thanks for yet another learning.

  • @lugiadark21
    @lugiadark21 Před 3 lety

    Everything good I just do not think you should call in line 18 the function removeNodes that returns a TreeNode without having an object that receives it. I know Java garbage collection will destroy it, but I think it is bad practice

  • @ppavantiwari
    @ppavantiwari Před 2 lety

    Its more like Post order traversal not DFS

  • @carlystern3025
    @carlystern3025 Před 4 lety +1

    You’re pretty

  • @janmichaelaustria620
    @janmichaelaustria620 Před rokem

    Yo I solved it the same way! Verbatim almost! Putting up a python implementation here for anyone else going through the LC google tagged problems!
    ```
    class Solution:
    def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:
    to_delete = set(to_delete)
    ans = []
    def delete(node):
    if not node:
    return None
    node.left = delete(node.left)
    node.right = delete(node.right)
    #if we need to delte
    if node.val in to_delete:
    #if there is a left, we want it to be a part of the first
    if node.left:
    ans.append(node.left)
    if node.right:
    ans.append(node.right)
    #we need to delete this node that we are one
    return None
    return node
    delete(root)
    #check root
    if root.val not in to_delete:
    ans.append(root)
    return ans
    ```