Same Tree (LeetCode 100) | Full solution with visuals and animations | Study Algorithms

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

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Two trees even if they are different can result in a same traversal result. This happens because of the non-linear structure of the tree data structure. A neat little trick can be applied to the level order traversal technique to determine if two trees are exactly the same. Watch this video to understand how you can take advantage of the actual structure of a tree node and arrive at a solution.
Actual Problem: leetcode.com/problems/same-tree/
Chapters:
00:00 - Intro
00:52 - Problem Statement and Description
02:58 - What exactly are same trees?
05:50 - A trick with level order traversal
09:02 - Dry-run of Code
12:57 - Final Thoughts
📚 Links to topics I talk about in the video:
LeetCode Problems: • Leetcode Solutions
Other easy problems: • Easy Problems
Level Order Traversal: • Level order traversal ...
Binary Tree: • Binary Trees Introduct...
📘 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

Пікірлер: 21
@shrirangjoshi6568
@shrirangjoshi6568 4 ай бұрын
No one has solved this in this way.Thank you!!
@floatingfortress721
@floatingfortress721 6 ай бұрын
Thanks, you were able to simplify the problem and its solution for me :)
@kunalkheeva
@kunalkheeva Жыл бұрын
Thank you for the edge cases.
@alisheheryar1770
@alisheheryar1770 3 ай бұрын
Godspeed Nikhil.
@luminraja09
@luminraja09 2 ай бұрын
I am a fan of BFS. Thanks Man for the solution. :)
@dineshdsv7815
@dineshdsv7815 6 ай бұрын
Thanks for the explanation!!
@nikoo28
@nikoo28 5 ай бұрын
Happy to help!
@subee128
@subee128 6 ай бұрын
Thanks
@brentdaniels5462
@brentdaniels5462 Жыл бұрын
Can you do a tutorial on the recusive solution? figiured out the itreative solution but I am still a little bit confused on how the recusive solution works.
@nikoo28
@nikoo28 Жыл бұрын
if you understand the iterative solution, that is all you need. A recursive solution if often confusing. If you still want a recursive solution, you might find one in the leetcode discuss section. I would not recommend it though
@smitjoshi7202
@smitjoshi7202 11 ай бұрын
Which one is better? recursive or iterative? Recursive solutions comes naturally to for trees usually.
@nikoo28
@nikoo28 11 ай бұрын
i usually prefer an iterative approach, as it is more easy to debug. Think about a scenario when you are trying to understand the flow and find what is going wrong. With a recursive approach, you will get stuck in cycles, and it gets very hard. At the end it comes down to preference.
@mdshafiuddin1234
@mdshafiuddin1234 10 ай бұрын
sir please make playlist on recursion this concept is root for all advance concept of dsa
@nikoo28
@nikoo28 9 ай бұрын
yes, I will add a playlist on recursion.
@leetcode650
@leetcode650 7 ай бұрын
Please do solution in python or make a poll :(
@nikoo28
@nikoo28 6 ай бұрын
try to understand the logic rather than concentrating on the language :)
@Amed-pf7dp
@Amed-pf7dp 3 ай бұрын
there is no need for the extra space
@nikoo28
@nikoo28 2 ай бұрын
What solution would you suggest?
@Amed-pf7dp
@Amed-pf7dp 2 ай бұрын
@@nikoo28 class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) { return true; } if (p != null && q != null && p.val == q.val) { return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } return false; } }
@philandros3195
@philandros3195 4 ай бұрын
I don't think I understand how the tree works. In the first test case the trees are q = [1, 2, 3] and p = [1, 2, 3] and if I write: return q.val == p.val and q.left == p.left and q.right == p.right, the output is False??? Also if I try just return q (for science), I would expect the output to be the tree [1,2,3], but instead the output is True?? What the fuck. How do these damn trees work?!?
@nikoo28
@nikoo28 3 ай бұрын
check out my complete playlist on trees, you will have a very good understanding.
Did you believe it was real? #tiktok
00:25
Анастасия Тарасова
Рет қаралды 17 МЛН
⬅️🤔➡️
00:31
Celine Dept
Рет қаралды 52 МЛН
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 610 М.
Introduction to Trees (Data Structures & Algorithms #9)
10:30
The Last Algorithms Course You'll Need by ThePrimeagen | Preview
16:44
Frontend Masters
Рет қаралды 309 М.
Leetcode 100. Same Tree [Java]
5:41
if else statement
Рет қаралды 1,9 М.
Did you believe it was real? #tiktok
00:25
Анастасия Тарасова
Рет қаралды 17 МЛН