Author |
Topic: Checking whether BST formed are carbon copy (Read 6370 times) |
|
Skandh
Junior Member
Gender:
Posts: 77
|
|
Checking whether BST formed are carbon copy
« on: Jan 13th, 2012, 8:46pm » |
Quote Modify
|
I had recently encountered this question in technical discussion. Given two integer arrays. If we form two binary search trees from each array, by inserting elements from array one by one, we need to return whether both binary search tree's are carbon copy of each other or not. Example : (1) Array1 : 5 4 7 Array2 : 5 7 4 Binary Search Tree constructed from Array1: 5 / \ 4 7 Binary Search Tree constructed from Array2: 5 / \ 4 7 Binary search tree constructed using these two arrays'll be carbon copy of each other. (2) Array1 : 5 6 7 Array2 : 5 7 6 Binary Search Tree for Array1 : 5 \ 6 \ 7 Binary Search Tree for Array2 : 5 \ 7 / 6 So these two trees are not carbon copy of each other. Solution which I came up with to form bst from each array ( after checking whether size of both array are equal or not etc) and then compare them. Is there any constant space solution for this problem possible?
|
|
IP Logged |
I wanna pull by legs!!!
|
|
|
ashmish2
Newbie
Posts: 12
|
|
Re: Checking whether BST formed are carbon copy
« Reply #1 on: Jan 29th, 2012, 12:55am » |
Quote Modify
|
isCarbon { for i 1 to N if( a[i] == b[i]) continue; else if(a[i] == b[i+1] && a[i+1] == b[i] && ( (a[i] - a[i/2] ) *( a[i+1]- - a[i/2] ) < 0) && ( (b[i] - b[i/2] ) *( b[i+1]- - b[i/2] ) < 0)) i++; else return false; endif end for return true; }
|
|
IP Logged |
|
|
|
akasina9
Newbie
x = 0x2B | ~ 0x2B. x is the question
Gender:
Posts: 9
|
|
Re: Checking whether BST formed are carbon copy
« Reply #2 on: Nov 16th, 2012, 2:30am » |
Quote Modify
|
on Jan 29th, 2012, 12:55am, ashmish2 wrote:isCarbon { for i 1 to N if( a[i] == b[i]) continue; else if(a[i] == b[i+1] && a[i+1] == b[i] && ( (a[i] - a[i/2] ) *( a[i+1]- - a[i/2] ) < 0) && ( (b[i] - b[i/2] ) *( b[i+1]- - b[i/2] ) < 0)) i++; else return false; endif end for return true; } |
| @ashmish2: Can you please explain your solution a bit.
|
|
IP Logged |
|
|
|
birbal
Full Member
Gender:
Posts: 250
|
|
Re: Checking whether BST formed are carbon copy
« Reply #3 on: Dec 13th, 2012, 11:17am » |
Quote Modify
|
on Jan 13th, 2012, 8:46pm, Skandh wrote:I had recently encountered this question in technical discussion. Given two integer arrays. If we form two binary search trees from each array, by inserting elements from array one by one, we need to return whether both binary search tree's are carbon copy of each other or not. Example : (1) Array1 : 5 4 7 Array2 : 5 7 4 Binary Search Tree constructed from Array1: 5 / \ 4 7 Binary Search Tree constructed from Array2: 5 / \ 4 7 Binary search tree constructed using these two arrays'll be carbon copy of each other. (2) Array1 : 5 6 7 Array2 : 5 7 6 Binary Search Tree for Array1 : 5 \ 6 \ 7 Binary Search Tree for Array2 : 5 \ 7 / 6 So these two trees are not carbon copy of each other. Solution which I came up with to form bst from each array ( after checking whether size of both array are equal or not etc) and then compare them. Is there any constant space solution for this problem possible? |
| How is the structure of a binary search tree defined? It has a property that left subtree of less than node and right subtree is greater than it. But this can give us different structures.
|
|
IP Logged |
The only thing we have to fear is fear itself!
|
|
|
birbal
Full Member
Gender:
Posts: 250
|
|
Re: Checking whether BST formed are carbon copy
« Reply #4 on: Dec 13th, 2012, 11:26am » |
Quote Modify
|
If we have two definate structures, comparison is simple. IsCarbonCopy(tree t1, tree t2) { if(!t1 && !t2) return true; if(t1 && t2) return t1->value == t2->value && IsCarbonCopy(t1->left, t2->left) && IsCarbonCopy(t1->right, t2->right); return false; }
|
|
IP Logged |
The only thing we have to fear is fear itself!
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Checking whether BST formed are carbon copy
« Reply #5 on: Dec 14th, 2012, 11:45am » |
Quote Modify
|
on Dec 13th, 2012, 11:26am, birbal wrote:If we have two definate structures, comparison is simple. |
| But we don't. And creating the two structures breaks the asked time-complexity-constraint.
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
Grimbal
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 7527
|
|
Re: Checking whether BST formed are carbon copy
« Reply #6 on: Dec 15th, 2012, 1:05pm » |
Quote Modify
|
I think I have a solution. The big problem is that the "constant space" restriction forbids recursive methods. So we need to do some iterations. The idea is to loop over each element, and reconstruct the path from the root to that element in both arrays and compare that they compare OK. public boolean isCarbonCopyTree(int a[], int b[]) { if( a.length!=b.length ) return false; for( int targetA=0 ; targetA<a.length ; targetA++ ){ int value = a[targetA]; // which occurrence is it? int n=0; for( int ia=0 ; ia<=targetA ; ia++ ){ if( a[ia]==value ) n++; } // find the same occurrence in b int targetB = 0; for( targetB=0 ; targetB<b.length ; targetB++ ){ if( b[targetB]==value){ if( --n==0 ) break; } } // if we reach the end of b, the corresponding value is missing if( targetB==b.length ) return false; // reconstruct and compare the path to a[targetA] and b[targetB] int ia=0, ib=0; int min = Integer.MIN_VALUE; int max = Integer.MAX_VALUE; while( ia<targetA || ib<targetB ){ // same subtree root? if( a[ia]!=b[ib] ) return false; // next subtree if( value<a[ia] ){ max = a[ia]; } else { min = a[ia]; } // find the roots of the subtrees in a and b do { ia++; } while( !(a[ia]>=min && a[ia]<max) ); do { ib++; } while( !(b[ib]>=min && b[ib]<max) ); } } // all values checked, return OK. return true; }
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Checking whether BST formed are carbon copy
« Reply #7 on: Dec 15th, 2012, 1:38pm » |
Quote Modify
|
If we can destroy the arrays, I bet we can do better on the time-complexity..
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
|