• 2 days ago
Transcript
00:00Next question, which we will talk about is the name of the subtree of another tree.
00:04We have two trees in the question.
00:06One is our main tree, which is the structure of something like this.
00:10And one is our subtree, which is the structure of something like this.
00:14If we have two roots and sub-root,
00:19we have to return true,
00:21if this is my sub-root in this tree,
00:23where to where lie.
00:26And I return false.
00:28If my whole sub-tree doesn't lie anywhere in my tree.
00:31A sub-tree completely lie in a binary tree,
00:35means that the structure should also be the same,
00:38and the node values should also be the same.
00:41If I take this tree and this sub-tree example,
00:44then my tree in this root,
00:47that is the two roots,
00:49it matches my sub-root.
00:52And with this two, the left child,
00:54is 4 and the right child is 5.
00:58That means the entire left sub-tree also matches the same,
01:01and the right sub-tree also matches the same.
01:03That's why,
01:04for this tree and tree,
01:06the answer will return the answer,
01:08that will be equal to true.
01:09But if we take this,
01:11here we have another node.
01:13After 4,
01:14I have left child,
01:166.
01:17Then my sub-tree,
01:19I have the main tree,
01:20I have the main tree,
01:21and I have the main tree,
01:21and we will return false.
01:23False,
01:23because this node,
01:26this node,
01:26this node,
01:26this node,
01:27it matches the same.
01:28Now the left child,
01:29it matches the same.
01:31And the right child 5,
01:32it matches the same.
01:34And the right sub-tree,
01:34they match the same.
01:35But no left child,
01:37but I have the main tree inside,
01:40never left child.
01:41So this is the whole tree,
01:44that entire tree left,
01:44this tree is not here,
01:46but there is also the same structure.
01:50the same structure is not going to happen.
01:51The same structure of the nodes,
01:53the nodes in the value is the same.
01:55The left sub-tree,
01:57the nodes in the nodes,
01:58they should have the same,
01:59the nodes in the same.
02:01They should have the same nodes
02:02in the same.
02:03same number of nodes with same number of structure and same value in nodes.
02:09If we consider here 4 is 3, and I don't have these nodes.
02:17In this case, this structure does not exist here.
02:22This means these 3 nodes exist in this form, but here 4 is not 3.
02:27That's why we return false.
02:29We take another case in which I have no node.
02:34We have another node here.
02:37So, what do I say in this case?
02:41This sub-tree has 2, this 4 and this 5.
02:48This sub-tree can be put here.
02:51But, this main tree in 4 is 6.
02:54So, this sub-tree will not become the sub-tree.
02:58Because the structure is not the same.
03:00If there are null values here, then it is the same.
03:04Because there is an extra node here.
03:06So, I need to return false here.
03:08So, what should I say?
03:09The sub-trees should be the same.
03:11The sub-trees should be the same.
03:12The sub-trees should be the same.
03:13It means that it is 2 to 2.
03:14It is 4 to 4.
03:16But, the sub-trees should be the same.
03:20The sub-trees should be the same.
03:21The left sub-trees should be the same.
03:23The right sub-trees should be the same.
03:24The right sub-trees should be the same.
03:25The left sub-trees should be the same.
03:26The right sub-trees should be the same.
03:28The left sub-trees should be the same.
03:30The right sub-trees should be the same.
03:46same node values exactly lie or do not.
03:50So, first of all, in our mind, when we try to look at the picture, we try to match the
03:57root
03:57first.
03:59What do I do?
04:00I try to match the root with it.
04:01I try to match the root with it.
04:03Does it happen here?
04:04It doesn't happen here.
04:05It doesn't happen here.
04:05It doesn't match the node value.
04:07So, first of all, my work is that I call the sub-root.
04:12I call it sub-root.
04:13I call it sub-root because it's a sub-root.
04:15This is the sub-root.
04:16I try to find out a matching node logically.
04:20So, first of all, I compare it with it.
04:23It doesn't match the other nodes.
04:25It doesn't match the other nodes.
04:26It doesn't match the value with it.
04:28Finally, the value of 2 will match my 2 with it.
04:32Then, what do I do?
04:33I see that the whole tree is my whole tree.
04:39If these two trees are identical,
04:42they are identical in terms of structures
04:44and identical in terms of node values.
04:47The structure is the same.
04:49The node values are the same.
04:50Then I call it identical.
04:52If I get the sub-root,
04:53I get the main tree in a node.
04:56If I get the sub-root and the sub-root
04:58and the sub-root are identical,
04:59then I can say that
05:01the tree is the main tree.
05:05Then I will return true.
05:07I have another example in which I have many nodes.
05:13This is 1, 2, 3, 4, 5 and 6.
05:17And I have another sub-tree in which I have a structure in which I have 5 and 6.
05:24So, what do I do?
05:25The sub-root is my sub-root.
05:26I try to find the main tree in my main tree.
05:29First, I match it with the root.
05:30Then, I try to match it with the root.
05:33I try to match it with the root.
05:34Then, I try to match it with the root.
05:35Then, I try to match it with the root.
05:49There is no match there.
05:54So, I try to match the root here.
06:01If the root is null, if I try to have the root here,
06:07then my structure will become ready.
06:09So logically my problem
06:11I have divided into two pieces
06:12The first part is
06:14To find my sub-root
06:18This is my node
06:20It is called sub-root
06:22Because it is sub-tree
06:23Find my sub-root in tree
06:27This sub-root
06:28I have to find my tree
06:29Where do I find my sub-root
06:31It is the second part
06:32Which is the problem
06:34It is check
06:37Identical
06:38I have to check who is
06:41Which is my whole sub-tree
06:43Here I have a tree
06:45Which is my whole sub-tree
06:47And with my node
06:51Sub-tree
06:52So my problem
06:54I have divided into two small pieces
06:56First part is logically
06:57First I have to find my sub-root
06:59To find my tree
07:00The second part is logically
07:02Which I have to check
07:03Where the node is the sub-tree
07:06And which is my original sub-tree
07:07They both are same
07:08They both are identical
07:10Now the first part is to find the sub-root
07:12To find out
07:12This is basically a problem
07:15Which I have given a node
07:16And I have to find the node
07:17And that node's value
07:18To find out
07:18So I have to find the node
07:20So I have to do a normal traversal
07:22On this tree
07:22And in traversal
07:23Every time I will compare the root
07:24To the root
07:26Which is the root
07:27Which is the data
07:27That is equal to my sub-root
07:30As the root of the data
07:31As the root of the sub-root
07:32To the root of the data
07:32To be equal
07:33As the root of the data
07:34So I will find the sub-root
07:35And I will find the sub-root
07:35And my main tree
07:36And I will find my sub-root
07:38And what I will do
07:38Before I will go to one
07:39And one
07:40To compare the two
07:41value in the same. Then I will go to left sub tree for 2 and compare. Here I will go
07:46to two values there. The same thing as I will do, the first part is complete. Same
07:51thing I will do. 5 will first compare the value 1 x equal to 5 is equal. Then I will
07:57go
07:57to left sub tree, 2 to 5 is equal not equal. Then I will go to left sub tree to
08:013 and
08:02we will not compare this to 5 and 4. Then I will go to left sub tree inside my sub
08:09root
08:09lie नहीं करती, उसके बाद 1 के
08:11right sub tree में जाओंगी, मेरे पास आएगी
08:13node number 5, तो यह 5 और इसका 5
08:15का data same हो जाता है, तो यहाँ पर
08:17मेरा first part complete हो गया, यानि
08:19मुझे मेरी sub root, मेरे main tree हैंदर
08:21मिल गई है, अब मुझे अपना second part complete
08:23करना है, इस identical को check करना
08:25यानि मुझे check करना है, मेरे दो
08:27trees identical हैं या नहीं हैं, इस
08:29में से पहला tree यह बन जाएगा, दूसरा
08:31tree यह बन जाएगा, अगर मैं original
08:33example की बात करूँ, तो पहला tree
08:35मेरे पास यह बन जाएगा, दूसरा tree
08:37मेरे पास यह बन जाएगा, इन दोनो
08:39trees के लिए मुझे देखना है, यह दोनो
08:40identical हैं या नहीं है, तो मैं सबसे
08:42simply क्या करूँगी, पहले check कर लूँगी
08:45इनके root की value, क्या root की value
08:47same है, अगर root की value
08:49same है, तो मतलब trees अभी तक तो
08:51identical है, फिर check कर लूँगी
08:53पूरे left subtree के लिए, अगर
08:55पूरे left subtree में
08:56सारी की सारी चीजें identical है
08:58तो मतलब अभी तक तो identical है
09:00फिर चेक कर लूँगी पूरे right subtree
09:03के लिए, अगर पूरा right subtree
09:05भी identical है, तो मतलब मेरे
09:06trees completely identical है
09:08या identical चेक करने से better है कि
09:10मैं non-identical की condition को
09:12पहचान लूँ, non-identical
09:14यानि कहां कहां पर मेरे trees
09:16identical नहीं होंगे, पहली
09:18condition जिसमें मेरे trees identical
09:20नहीं होंगे वो यह होगा कि मेरे
09:22node का जो data है
09:27वो मेरे
09:28subroot के
09:30data के equal ना हो
09:31मतलब जिस subroot से मैं compare कर रही हूँ
09:34उसके data के equal ना हो
09:36तब मेरे trees के आउंगे non-identical
09:38बन जाएंगे
09:40कहने का मतलब यह है कि अगर यहां पर
09:422 की जगे 1 होता, तो मैं
09:44इस 2 को जब इस 1 से compare करती
09:46तो मेरे subtrees क्या हो जाते, non-identical
09:48हो जाते, या फिर यहां पर
09:502 होता, यहां पर 5 की जगे मेरे पास
09:527 होता, तो यह तो identical
09:54हो जाते nodes, यह भी identical हो जाते
09:56पर जब मैं 5 को 7 के साथ
09:58compare करती, तो यह non-identical हो जाते
10:00so this is the first condition when my sub-trees are non identical
10:04the second condition is what he can say
10:06the second condition is when the two are null
10:09or my node null
10:12or my sub-root null
10:15when the two are null
10:18the node is null and sub-root is null
10:21this left child and this left child
10:24will be identical
10:25we will take this here
10:27here is null
10:29then the two two
10:30when I go to left sub-trees
10:33here is null
10:34here is null
10:37here is null
10:39here is null
10:40in that case
10:41the trees are non-identical
10:43the third case
10:47in which they are non-identical
10:48that I have seen the left sub-trees
10:52and left sub-trees
10:53non-identical
10:55or my left sub-trees
10:56non-identical
10:57means that my whole tree
11:01is non-identical
11:03for this condition
11:04for this condition
11:05for this condition
11:05for this condition
11:06for this condition
11:13and I have returned
11:14to the right sub-trees
11:17and I have checked the right sub-trees
11:20and my right sub-trees
11:22is non-identical
11:23this means that
11:25in the right sub-trees
11:26in the 5
11:26there was no 7
11:29what did
11:302 has done?
11:302 is identical
11:312 has been identical
11:332 has been identical
11:332 has been identical
11:36but there is no 7
11:41because in the 7
11:42there exists this node
11:43and here is null
11:44so this is why
11:46either the left sub-trees
11:47non-identical
11:48or the right sub-trees
11:49non-identical
11:50or the other nodes
11:51between the two nodes
11:52or the other
11:53or the other
11:54not equal
11:54these are 4 conditions
11:56for any root
11:57or any tree
11:59non-identical
12:00so I have two work in my code
12:02first of all
12:03I have to find the sub-root
12:05in my tree
12:06and find the traversal
12:07use and find the sub-root
12:08we can find the tree
12:09in the other
12:10another work
12:10that I have to check
12:12for identical
12:12where I have a node
12:14like with 2
12:15I have to find the node
12:16so I have to check
12:19for the other
12:20recursive function
12:21which I have to check
12:22for every level
12:23which I have to check
12:23for the data
12:25or the other
12:26is null
12:27or the left sub-trees
12:29non-identical
12:30or the right sub-trees
12:31non-identical
12:32or the right sub-trees
12:33non-identical
12:34so I will return false
12:35in that case
12:36I will return false
12:37if my tree is identical
12:38so I will return true
12:39for this
12:40now we are going to write
12:41code
12:41code
12:41write
12:41code
12:41I will return
12:42to the next
12:42three