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
Thanks for the tip about doing the 40 tree problems at the end.
Bring back this Nick White. The cool chill one that just helps out
got the logic and solved the problem just after 3:27. thanks bro.
Nick helped code 911 and grinder
Great Work bro
Can you make a basic lecture on Binary Tree explaining the traversals with dry run in Trees ?
Very clear. Thanks
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.
line 23, it should be t1.right = mergeTrees(t1.right, t2.right);
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".
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
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
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.
You are brilliant good sir!
wait why is he not indian😂😂?
why this problem is in easy level
its looking too difficult man
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.
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
This is just the DFS traversal of the tree.
Clean and Simple
ty
You rock, Thanks!
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!!
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.
28 of January of 2023.
Leetcode hasn't fix that yet
it's soooo easy =( But I cant solve even that problem... what's wrong with me GOD DAMN
I hate it when he says that