Same Binary Tree

  Рет қаралды 45,087

Tushar Roy - Coding Made Simple

Tushar Roy - Coding Made Simple

Күн бұрын

Пікірлер: 26
@maxbarahona9675
@maxbarahona9675 5 жыл бұрын
My man Tushar is the God of programming
@shankysays
@shankysays 5 жыл бұрын
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.
@jdragon8184
@jdragon8184 3 жыл бұрын
its almost intership time , wether i get one or not , tushar was a big part of my journey
@shivtanwar3054
@shivtanwar3054 6 жыл бұрын
The way you teach is protean and I liked it so much thanks
@avinashaitha143
@avinashaitha143 8 жыл бұрын
Welcome back to the Gabage
@rajesh21602
@rajesh21602 5 жыл бұрын
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.
@dineshgarg9567
@dineshgarg9567 5 жыл бұрын
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.
@prajwalchoudhary4824
@prajwalchoudhary4824 4 жыл бұрын
@@dineshgarg9567 thanks
@madhukiranattivilli2321
@madhukiranattivilli2321 3 жыл бұрын
cond1 && cond2 && cond3 would be true only if all 3 conditions are true
@ManishPandey-mn3gl
@ManishPandey-mn3gl 9 жыл бұрын
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 ?
@gokulnaathb2627
@gokulnaathb2627 3 жыл бұрын
This was so good, Sir
@bugrahanabdulkarim6034
@bugrahanabdulkarim6034 6 жыл бұрын
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); }
@TheGamerGuy201
@TheGamerGuy201 7 жыл бұрын
can we write :- if(root1.data == root2.data){ return areIdentical(root1.left,root2.left) && areIdentical(root1.right,root2.right)? }
@priyankpande9
@priyankpande9 6 жыл бұрын
Yes it is correct, because in the video if root1.data==root2.data value will be false then it will not go beyond && operation.
@mystic3662
@mystic3662 6 жыл бұрын
Can we also perform in-order and (pre-order or post-order) traversal of both trees and compare if they are equal?
@bharatgupta3287
@bharatgupta3287 6 жыл бұрын
yes but recursion method has less time complexity
@disenchanted_dove
@disenchanted_dove 5 жыл бұрын
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
@sauravagrawal5013
@sauravagrawal5013 5 жыл бұрын
@@disenchanted_dove We can compare inorder, preorder and postorder sequentially. If all match, then same.
@akarshit8556
@akarshit8556 3 жыл бұрын
@@sauravagrawal5013 It is failed on certain testcases. like [1,1] and [1,null,1].
@mr_world_wide
@mr_world_wide 5 жыл бұрын
Why can't Two be Two , why does it have to be Thwo
@pramoth805
@pramoth805 4 жыл бұрын
You are little bit faster. Could you please teach a little bit slower
@rajputtechclasessrtc6876
@rajputtechclasessrtc6876 5 жыл бұрын
challange for u 2,4,6,3,7,7,8,10 create binary tree 😎
@sauravagrawal5013
@sauravagrawal5013 5 жыл бұрын
I think U mean BST ?? Not BT. hehehe
Size Of Binary Tree
4:49
Tushar Roy - Coding Made Simple
Рет қаралды 49 М.
Check if Binary Tree is Binary Search Tree
10:29
Tushar Roy - Coding Made Simple
Рет қаралды 122 М.
ROSÉ & Bruno Mars - APT. (Official Music Video)
02:54
ROSÉ
Рет қаралды 102 МЛН
pumpkins #shorts
00:39
Mr DegrEE
Рет қаралды 125 МЛН
Mom had to stand up for the whole family!❤️😍😁
00:39
Сюрприз для Златы на день рождения
00:10
Victoria Portfolio
Рет қаралды 2,4 МЛН
Data structures: Binary Tree
16:17
mycodeschool
Рет қаралды 1,4 МЛН
Lowest Common Ancestor Binary Tree
11:08
Tushar Roy - Coding Made Simple
Рет қаралды 252 М.
10.1 AVL Tree - Insertion and Rotations
43:08
Abdul Bari
Рет қаралды 1,2 МЛН
AVL Trees & Rotations (Self-Balancing Binary Search Trees)
20:38
Back To Back SWE
Рет қаралды 343 М.
Fenwick Tree or Binary Indexed Tree
22:43
Tushar Roy - Coding Made Simple
Рет қаралды 238 М.
Data structures: Binary Search Tree
19:28
mycodeschool
Рет қаралды 1,3 МЛН
Iterative Postorder Traversal of Binary Tree
8:29
Tushar Roy - Coding Made Simple
Рет қаралды 79 М.
Binary Trees - Data Structures Explained
10:18
Aaron Jack
Рет қаралды 132 М.
The Traveling Salesman Problem: When Good Enough Beats Perfect
30:27
ROSÉ & Bruno Mars - APT. (Official Music Video)
02:54
ROSÉ
Рет қаралды 102 МЛН