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.
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
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.
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?!?
No one has solved this in this way.Thank you!!
Thanks, you were able to simplify the problem and its solution for me :)
I am a fan of BFS. Thanks Man for the solution. :)
awesome ❤
Thank you for the edge cases.
Thanks for the explanation!!
Happy to help!
Godspeed Nikhil.
Thanks
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.
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
Which one is better? recursive or iterative? Recursive solutions comes naturally to for trees usually.
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.
sir please make playlist on recursion this concept is root for all advance concept of dsa
yes, I will add a playlist on recursion.
Please do solution in python or make a poll :(
try to understand the logic rather than concentrating on the language :)
there is no need for the extra space
What solution would you suggest?
@@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;
}
}
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?!?
check out my complete playlist on trees, you will have a very good understanding.