we can also do it like this. declare a global bool value as true. do any tree transversal and in visit stage make the bool value false as we encounter non equal value. at the end just check the bool value if its still true means the btree are equal or viceversa.
@jdragon81843 жыл бұрын
its almost intership time , wether i get one or not , tushar was a big part of my journey
@shivtanwar30546 жыл бұрын
The way you teach is protean and I liked it so much thanks
@avinashaitha1438 жыл бұрын
Welcome back to the Gabage
@rajesh216025 жыл бұрын
return root1.data==root2.data, do we need to change it to if condition, because it would return and end the code at the root it self if they are same and will never get to the recursive part. Please let me know.
@dineshgarg95675 жыл бұрын
Nah, the code is correct. If you read carefully you'll see there's an AND operator between the return statements and the recursive function call statements.
@prajwalchoudhary48244 жыл бұрын
@@dineshgarg9567 thanks
@madhukiranattivilli23213 жыл бұрын
cond1 && cond2 && cond3 would be true only if all 3 conditions are true
@ManishPandey-mn3gl9 жыл бұрын
Hey Tushar, Nice solution! you can write it succintly like the following, public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null || q == null) return (p == q); return isSameTree(p.left, q.left) && isSameTree(p.right, q.right) && p.val == q.val; } In the code above, if(p == null || q == null) return (p == q); is equivalent to writing if(p == null && q == null) return true; else if(p == null || q == null) return false; I have a question here, You mentioned that the running time is O(n) here 1) What is n here. Is it total number of nodes in both tree? 2) Shouldn't it be O(min(m, n)) where m = number of nodes in tree1 and n = number of nodes in tree2 ?
@gokulnaathb26273 жыл бұрын
This was so good, Sir
@bugrahanabdulkarim60346 жыл бұрын
or you could write it like this public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null || q == null) return (p == q); return (p.val == q.val) && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); }
@TheGamerGuy2017 жыл бұрын
can we write :- if(root1.data == root2.data){ return areIdentical(root1.left,root2.left) && areIdentical(root1.right,root2.right)? }
@priyankpande96 жыл бұрын
Yes it is correct, because in the video if root1.data==root2.data value will be false then it will not go beyond && operation.
@mystic36626 жыл бұрын
Can we also perform in-order and (pre-order or post-order) traversal of both trees and compare if they are equal?
@bharatgupta32876 жыл бұрын
yes but recursion method has less time complexity
@disenchanted_dove5 жыл бұрын
There are different tree configurations possible for a single tree traversals results , therefore I don't think it would be right to simply compare the traversals
@sauravagrawal50135 жыл бұрын
@@disenchanted_dove We can compare inorder, preorder and postorder sequentially. If all match, then same.
@akarshit85563 жыл бұрын
@@sauravagrawal5013 It is failed on certain testcases. like [1,1] and [1,null,1].
@mr_world_wide5 жыл бұрын
Why can't Two be Two , why does it have to be Thwo
@pramoth8054 жыл бұрын
You are little bit faster. Could you please teach a little bit slower
@rajputtechclasessrtc68765 жыл бұрын
challange for u 2,4,6,3,7,7,8,10 create binary tree 😎