LeetCode Merge Two Binary Trees Solution Explained - Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ต.ค. 2024
  • The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
    Join my free exclusive community built to empower programmers! - www.skool.com/...
    Preparing For Your Coding Interviews? Use These Resources
    --------------------
    (My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
    AlgoCademy - algocademy.com...
    Daily Coding Interview Questions - bit.ly/3xw1Sqz
    10% Off Of The Best Web Hosting! - hostinger.com/...
    Follow Me on X/Twitter - x.com/nickwhit...
    Follow My Instagram - / nickwwhite
    Other Social Media
    ----------------------------------------------
    Discord - / discord
    Twitch - / nickwhitettv
    TikTok - / nickwhitetiktok
    LinkedIn - / nicholas-w-white
    Show Support
    ------------------------------------------------------------------------------
    Patreon - / nick_white
    PayPal - paypal.me/nick....
    Become A Member - / @nickwhite
    #coding #programming #softwareengineering

ความคิดเห็น • 26

  • @basedonprinciple
    @basedonprinciple 4 ปีที่แล้ว +16

    Thanks for the tip about doing the 40 tree problems at the end.

  • @Kitchen-Raccoon4572
    @Kitchen-Raccoon4572 10 หลายเดือนก่อน

    Bring back this Nick White. The cool chill one that just helps out

  • @sadmanabedin1203
    @sadmanabedin1203 4 ปีที่แล้ว +3

    got the logic and solved the problem just after 3:27. thanks bro.

  • @ConwellConwell
    @ConwellConwell ปีที่แล้ว +2

    Nick helped code 911 and grinder

  • @muscle_coder
    @muscle_coder ปีที่แล้ว

    Great Work bro
    Can you make a basic lecture on Binary Tree explaining the traversals with dry run in Trees ?

  • @Emmanuel-px9lk
    @Emmanuel-px9lk 5 ปีที่แล้ว +5

    Very clear. Thanks

  • @abhaypatil2000
    @abhaypatil2000 4 ปีที่แล้ว +3

    The problem you mentioned is not with leetcode. The problem is with multithreading at hardware level. Executing same program multiple times, will result in different time.

  • @mdaminulhoque1707
    @mdaminulhoque1707 2 ปีที่แล้ว +1

    line 23, it should be t1.right = mergeTrees(t1.right, t2.right);

  • @jay31415
    @jay31415 4 ปีที่แล้ว +2

    This question is awkwardly and ambiguously worded-a pattern I've noticed in LeetCode tree questions-as evidenced by bad grammar in the first sentence. The entire question would have been better off just showing the example on its own, ditching the misleading wording, instead simply saying "do this".

  • @snake1625b
    @snake1625b 2 ปีที่แล้ว

    oh i thought you had to insert all the nodes from the right into the left tree, not add the overlapping ones. should've read the question

  • @xbeta84
    @xbeta84 4 ปีที่แล้ว +4

    This is mine, similar to yours, but I don't merge into t1 to make it more readable
    def merge_trees(t1, t2):
    """
    :type t1: TreeNode
    :type t2: TreeNode
    :rtype: TreeNode
    """
    if t1 == None:
    return t2
    if t2 == None:
    return t1
    node = TreeNode(t1.val + t2.val)
    node.left = merge_trees(t1.left, t2.left)
    node.right = merge_trees(t1.right, t2.right)
    return node

    • @boshiij3449
      @boshiij3449 4 ปีที่แล้ว

      This is good but the interviewer might ask to reduce space by merging in-place instead of creating a new binary tree. Good to know and understand both though.

  • @noctua7771
    @noctua7771 ปีที่แล้ว

    You are brilliant good sir!

  • @sarf_alam
    @sarf_alam 3 ปีที่แล้ว +12

    wait why is he not indian😂😂?

  • @DEEPAKGOYALnullRA
    @DEEPAKGOYALnullRA 4 ปีที่แล้ว +5

    why this problem is in easy level
    its looking too difficult man

    • @botdot7226
      @botdot7226 4 ปีที่แล้ว +1

      Yeah I agree. My theory is that it rates the problems based on the % accepted rate. As you can see 136k/200k submissions passed meaning ~68% completed this problem. However, this is problem 617 meaning that only really dedicated users will have attempted this problem as fewer people will have likely made it past the first couple questions. Therefore, in this 200k is likely to be very experienced coders so the difficulty rating is misleading.

    • @spectermakoto9029
      @spectermakoto9029 4 ปีที่แล้ว +3

      you have to really understand cs fundamentals not a difficult problem if you do have problems i recommend you brush up on trees and recursion

    • @jugsma6676
      @jugsma6676 4 ปีที่แล้ว

      This is just the DFS traversal of the tree.

  • @vishwajeetbarve6538
    @vishwajeetbarve6538 3 ปีที่แล้ว +1

    Clean and Simple

  • @cocoarecords
    @cocoarecords 4 ปีที่แล้ว

    ty

  • @eshagupta9407
    @eshagupta9407 4 ปีที่แล้ว

    You rock, Thanks!

  • @TheodoreRavindranath
    @TheodoreRavindranath 3 ปีที่แล้ว +4

    For an experienced developer, this looks scary.. because we prefer immutability, and clean copies. All this t1.val+=t2.val thereby mutating the input tree, and just grabbing some nodes off another tree and keeping them in our tree... all this looks scary!!

    • @bigblue82
      @bigblue82 2 ปีที่แล้ว +2

      Agreed! When I first attempted this I assumed (wrongly, but based on experience) that the input trees should not be mutated. So I created an entire new tree and left the input trees untouched which made my solution more complicated. This one is far more simple, but it mutates t1 which feels so gross to me.

  • @isanrodrigueztrimino6701
    @isanrodrigueztrimino6701 ปีที่แล้ว

    28 of January of 2023.
    Leetcode hasn't fix that yet

  • @spinacker16
    @spinacker16 4 ปีที่แล้ว +2

    it's soooo easy =( But I cant solve even that problem... what's wrong with me GOD DAMN