Subtree of Another Tree (LeetCode 572) | Full solution with examples | Study Algorithms

  Рет қаралды 5,763

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Actual Problem: leetcode.com/problems/subtree...
Chapters:
00:00 - Intro
00:44 - Problem Statement and Description
02:27 - Efficient Solution
07:56 - Caveats and Gotchas
11:09 - Dry-run of Code
13:11 - Final Thoughts
📚 Links to topics I talk about in the video:
Pre-order Traversal: • Pre-order traversal of...
Post-order Traversal: • Post-order traversal o...
In-order Traversal: • In-order traversal of ...
Level order Traversal: • Level order traversal ...
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/36pU0JO
Favorite book to understand algorithms: amzn.to/39w3YLS
Favorite book for data structures: amzn.to/3oAVBTk
Get started for interview preparation: amzn.to/39ysbkJ
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
🎥 My Recording Gear:
Recording Light: amzn.to/3pAqh8O
Microphone: amzn.to/2MCX7qU
Recording Camera: amzn.to/3alg9Ky
Tablet to sketch and draw: amzn.to/3pM6Bi4
Surface Pen: amzn.to/3pv6tTs
Laptop to edit videos: amzn.to/2LYpMqn
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview

Пікірлер: 36
@unemployedcse3514
@unemployedcse3514 Ай бұрын
guys who ever is confused with '^' in Stringbuilder it is to handle below kind of tree node TreeNode root = new TreeNode(12); TreeNode subRoot = new TreeNode(2); where 12 (tweleve) will be treated as 1,2 subtree as 2 it gives true to figure out item or data in node we are using ^ as seperator still if u need confirmation pls refer test case created by nikhil in description code and nikhil small suggestion pls explain or stick to same logic during dry run , this will give more clarification and reduces doubts , in last example no null were included in ur explanation rest vedio is very helpful thank you ❤
@AlexTopGT
@AlexTopGT 9 ай бұрын
thank you! best explanation.
@shrirambalaji2915
@shrirambalaji2915 Жыл бұрын
Thank you brother
@rahulsinghdeo7519
@rahulsinghdeo7519 Жыл бұрын
Hey can you please explain how the "sb" object of String builder is preserve-ing the value when it moves to next iteration.
@syedomarali2678
@syedomarali2678 16 күн бұрын
It won't take O(n). Java's .contains() works in O(MN). Use efficient pattern-matching algorithms like KMP to achieve linear TC.
@varundwivedi1079
@varundwivedi1079 8 ай бұрын
Great.Thanks
@alisheheryar1770
@alisheheryar1770 4 ай бұрын
you have to add string("^") to your initial string otherwise it breaks at special cases like s = 12nullnull t = 2nullnull Cheeky Nikhil.
@garvitarora7777
@garvitarora7777 3 ай бұрын
thanks bro
@smridhjain4100
@smridhjain4100 2 ай бұрын
Whenever you put StringBuilder sb = new StringBuilder("^"); it passes the test cases but if you dont put the ^ it fails a single test case of root =[1,2] and subroot=[2] , can you explain why please? I think this is the same query that others trying to ask in the comment section
@nikoo28
@nikoo28 2 ай бұрын
you have to add string("^") to your initial string otherwise it breaks at special cases like s = 12nullnull t = 2nullnull
@codeLine7
@codeLine7 10 ай бұрын
very nice explanation.
@nikoo28
@nikoo28 9 ай бұрын
Keep watching
@killeraloo3247
@killeraloo3247 7 ай бұрын
OP solution. 🧡 from remote.
@yemeniabedontaclf
@yemeniabedontaclf Жыл бұрын
Repeated DNA Sequences. make vedio about this
@user-bc6ss6gp3z
@user-bc6ss6gp3z 4 ай бұрын
Solve "node at distance k from target node" Please🙏
@djpsn7094
@djpsn7094 11 ай бұрын
Why we took ^ in StringBuilder
@nikoo28
@nikoo28 11 ай бұрын
it is just an anchor
@shahriarbadhon4596
@shahriarbadhon4596 8 ай бұрын
Nice
@pratyushtripathy1463
@pratyushtripathy1463 4 ай бұрын
Only pre-Order traversal will work here, In other 2 (POT & IOT) all TCs will not pass
@minnikeswarrao6185
@minnikeswarrao6185 Жыл бұрын
Time complexity shouldn't be O(n) ...since String.contains() takes O(m+n)
@nikoo2805
@nikoo2805 Жыл бұрын
O(m + n) will translate to O(n), where n > m
@pranavm9301
@pranavm9301 6 ай бұрын
O(m+n) is still O(n) since m O(2n) which is worst case when n=m, which is still O(n)
@sheikhmkrifat7749
@sheikhmkrifat7749 3 ай бұрын
i applied the same way in inorder and postorder after that it doesn't pass all the test cases in leetcode.
@aryavardhansingh2162
@aryavardhansingh2162 3 ай бұрын
same here do you know the reason ?
@sheikhmkrifat7749
@sheikhmkrifat7749 3 ай бұрын
@@aryavardhansingh2162 yeah, i found that preOrder is used for comparing structure of BT, cause here root node comes first , then left and right on the other hand inorder and post dosent follow this, for that preOrder is used to compare two BT.
@blacklight8932
@blacklight8932 Жыл бұрын
didnt work for all testcases
@nikoo28
@nikoo28 Жыл бұрын
Check the code in description. It passes on leetcode
@amanpahari690
@amanpahari690 3 ай бұрын
love ur explanion v much,, +1 suscription from my side buddy!!
@garvitarora7777
@garvitarora7777 3 ай бұрын
this is failing in this test case sir Input root =[12] subRoot =[2]
@nikoo28
@nikoo28 3 ай бұрын
What output are you expecting?
@BACSShaileshShettar
@BACSShaileshShettar 5 ай бұрын
test cases failing
@nikoo28
@nikoo28 4 ай бұрын
check the code in the github link provided in video description.
@sumeersaifi6354
@sumeersaifi6354 6 ай бұрын
you code is not fully correct if there is root 12 and subroot 2 then for 12 it will be 12nullnull and for will be 2nullnull and when you check root.contain(subroot) it will give true but it should be false
@nikoo28
@nikoo28 5 ай бұрын
it passes the constraints on leetcode. can you explain your test case a little better?
@sumeersaifi6354
@sumeersaifi6354 5 ай бұрын
I need to check will let you know
@thor1626
@thor1626 4 ай бұрын
will fail only one test case. Anyways this method is not recommended, good explanation tho.
World’s Largest Jello Pool
01:00
Mark Rober
Рет қаралды 110 МЛН
Amazing weight loss transformation !! 😱😱
00:24
Tibo InShape
Рет қаралды 67 МЛН
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 17 МЛН
Smart Sigma Kid #funny #sigma #comedy
00:40
CRAZY GREAPA
Рет қаралды 33 МЛН
Subtree of Another Tree
10:47
Kevin Naughton Jr.
Рет қаралды 59 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 633 М.
World’s Largest Jello Pool
01:00
Mark Rober
Рет қаралды 110 МЛН