Vishal S Kumar
BAN USERpublic Node* reverse(Node* head)
{
Node *rev=NULL;
while(head!=NULL)
{
curr = head;
head = head->next;
curr->next=rev;
rev=curr;
}
return rev;
}
The Inorder of BSTs will be equal if both trees contain same elements .
We can check if two BSTs contains same elements by XORing all the elements in the tree with the other .
public boolean isBSTEqual(BinaryTree *root1,BinaryTree *root2){
int xorOfBST1=getXOROFTree(root1);
int xorOfBST2=getXOROFTree(root2);
return xorOfBST1==xorOfBST2 ? true : false;
}
public int getXOROFTree(BinaryTree *root){
if(root==null) return 0;
return getXOROFTree(root->left) ^ getXOROFTree(root->right) ^ root->data;
}
it has o(n^2) complexity
- Vishal S Kumar February 12, 2014int maxProfit(int arr[])
{
int max_profit = arr[1] - arr[0];
int min = arr[0];
for(int i = 1; i < arr.length; i++)
{
if((arr[i] - min) >max_profit)
max_profit = arr[i] - min;
if(arr[i] < min)
min = arr[i];
}
return max_profit;
}
Here in question it is mentioned that there is no prior information about the values of M and N. So there is no range specified.
- Vishal S Kumar January 30, 2014Perfect !!!
- Vishal S Kumar January 24, 2014Good Solution
- Vishal S Kumar January 23, 2014Time Complexity ?
- Vishal S Kumar January 02, 20141. Create a circular List of one of them.
2.Then try to find start of loop in Linked List.
3.This node would be their merge point.
4.Now restore the List.
- Vishal S Kumar February 07, 2017