Interview Question
Applications DevelopersCountry: India
Interview Type: In-Person
why both inorder and preorder? can you think of an example where just inorder wont be enough?
Inorder & PreOrder take O(n) time.
After that substring search takes O(n) time with KMP algorithm.
So in effect, O(n) time we could verify this.
@getprith... doing only the inorder will not work because different structures of a tree may result in same inorder, which wont be considered as subtree because it
should be same regarding structure too. (same for only preorder )
A counter-example
A
/ \
B C
/ \ \
D E F
PreOrder: A B D E C F
InOrder: B D E A C F
A
/ \
B C
\
E
PreOrder: A B E C
InOrder: B E A C
@Anonymous: A subtree is defined as
A subtree of a tree T is a tree consisting of a node in T and all of its descendants in T.
In the example you have given, the second tree is not a subtree of the first.
For example, in the following case, tree S is a subtree of tree T.
Tree S
10
/ \
4 6
\
30
Tree T
26
/ \
10 3
/ \ \
4 6 3
\
30
/* Check if the data of both roots is same and data of left and right
subtrees are also same */
return (root1->data == root2->data &&
areIdentical(root1->left, root2->left) &&
areIdentical(root1->right, root2->right) );
}
/* This function returns true if S is a subtree of T, otherwise false */
bool isSubtree(struct node *T, struct node *S)
{
/* base cases */
if (S == NULL)
return true;
if (T == NULL)
return false;
/* Check the tree with root as current node */
if (areIdentical(T, S))
return true;
/* If the tree with root as current node doesn't match then
try left and right subtrees one by one */
return isSubtree(T->left, S) ||
isSubtree(T->right, S);
}
i wasn't able to think of anything in the interview room but after coming out i realized.
{
public class BinaryTree {
Integer content;
BinaryTree left;
BinaryTree right;
@Override
public String toString() {
// program to return a string representation of this tree;
return content + ((left == null) ? "" : left.toString())
+ ((right == null) ? "" : right.toString());
}
}
}
if you toString both Trees you can just check if one is a substring of the other
The question doesn't say anything about any specific tree, son the assumption of the binary tree cannot be justified. An O(n) time, O(n) space algorithm would be :
1.Hash the tree nodes
2. Hash the subtree in the same hash and if there is any subtree node whose entry is not occupied, return 0 (i.e., not a subtree)
* n is the total number of nodes in the original tree
We can check for inorder traversal of both the tree simultaneously within the same function
Let us suppose we iterate the main tree in preorder traversal such that we have the head node of the subtree and the current node of the main tree to have the same values.
Now call the following function
bool SearchSubtree(tree * HeadMain , tree* HeadSubtree) // HeadMain->value = HeadSubtree->value
{
If (!HeadSubtree)
return true;
if ((HeadMain == NULL || HeadMain != HeadSubtree) && (HeadSubtree != NULL))
return false;
if ((SearchSubtree(HeadMain->left , HeadSubtree->left)) == false)
return false;
return (SearchSubtree(HeadMain->left , HeadSubtree->left)) == false) ;
}
So what I'm trying to do is
1)Check if the nodes do not match , subtree is not present
2)If the nodes match , check the left subtree of both
3)Then check the right subtree
4)Now we dont have to traverse the Main tree completely . Eg. We'll keep on calling the left child of the main tree recursively only to the point the left child of the subtree has been completely traversed . We recursively call back the previous nodes for both . In essence we are traversing only that part of the main tree which is concerned with the subtree .
5)Extra memory is saved from copying each elsement and checking them later
6)The complexity O(N) where n corresponds to the number of elements of the subtree
let inorder traversal of Tree is S1
- ashwitha93 June 24, 2013inorder traversal of SubTree is S2
pre order traversal of Tree is S3
pre order traversal of Sub Tree is S4
now check if s2 is substring of s1 and s4 is substring of s3