## aaman.singh85

BAN USERgood approach.

- aaman.singh85 April 14, 2013@ Sonesh,can we modify the node structure? Can we add one extra sibling pointer?

- aaman.singh85 April 14, 2013what about adding a constructor??...suppose i add a parameterised constructor, will get errors if some base class exists, as base class wil have to provide the values for this new const

- aaman.singh85 March 04, 2013Inorder gives sorted array, use two pointer approach to find the sum, becomes linear (2n).

- aaman.singh85 February 22, 2013awsome!!

- aaman.singh85 October 20, 2012i did exactly the same way, just a correction

left->ritechild = current;

also u can skip explictly eqauting to NULL as they are altready pointing to NULL.

char *arrayrec(char arr[],int N,int i=0)

{

if(i>=N/2)

return arr;

char temp=arr[i];

arr[i]=arr[N-i-1];

arr[N-i-1]=temp;

arrayrec(arr,N,++i);

}

here is the simple tail recursive version.

char* strcp(char*a,char*b,int i=0)

{

b[i]=a[i];

if(a[i]=='\0')

return b;

strcp(a,b,++i);

}

pivot element will always be smaller then its left and right elemnets.so if middle element is smaller then the 1st element of the subarray, traverse left, else if midle element is greater then the first, traverse right.also when only one element left, thats the pivot

- aaman.singh85 October 18, 2012oops forgot to login.pasting code again.

fun(node*n,int m int n))

{ if(n)

{ if(n->data>=m&& n->data<=n)

{ cout<<n->data;

if(n->left)

fun(n->left);

if(n->rite)

fun(n->rite);

}

if(n->data>m)

fun(n->left);

if(n->data<m)

fun(n->rite);

}

}

int _tmain(int argc, _TCHAR* argv[])

{

int i=0,j=0;

char str[]="spa ci ous string occ ur ed";

while(i<strlen(str))

{

if(str[i]==' ')

{

i++;

}

else

{

str[j]=str[i];

++i;

++j;

}

}

str[j]='\0';

std::cout<<str;

getch();

return 0;

}

non recursive:

adding all the nodes at each level in a stack and checking the length of the stack.

If its 2 to the power level, at all levels ,we get a complete binary tree.

bool iterativ_check(node* root)

{

stack<node*> st1; // can also take qeue

st1.push(root);

while(st1.size!=0)

{ stack<node*>st2(st1);

st1.clear();

whille(st2.size!=0)

{ node* n1=st2.pop();

if(n1->left!=Null&&n2->rite!=NULL)

{

st1.push(st1[i].left)

st1.push(st1[i].rite)

st2.pop();

}

}

if(st1.size!=Math.power(count,2);

break;

}

node* trees_programs::newtree(node* n)

{ node*n1=0;

node*n2=0;

if(n==0)

return NULL;

n1=newtree(n->left);

n2=newtree(n->rite);

node* nd=new node(0);

if(n==root)

{

root2=nd; // root of new tree

}

nd->left=n1;

nd->rite=n2;

nd->data=0;

if(n->left==0&&n->rite==0)

{

nd->data=0;

nd->left=0;

nd->rite=0;

}

if(n->left!=0)

nd->data+=n->left->data;

if(n->rite!=0)

nd->data+=n->rite->data;

if(n1!=0)

nd->data+=n1->data;

if(n2!=0)

nd->data+=n2->data;

return nd;

}

only if all values positive :)

- aaman.singh85 October 06, 2012this is correct, post order will calculate child sum and then go to parent. but the issue is we need to return node*, with this approach we will have to change all the parent nodes to sum of child nodes, not sure if allowed to do so .

- aaman.singh85 October 05, 2012y wd u chk node left if n->data<value?

- aaman.singh85 October 05, 2012fun(node* root,node*start,int k)

{

static int flag=0,level= 0;

if(n!=NULL)

{

if(root==start) // start node found now proceed

{ flag=1;

}

if( flag==1)

{

level++;

if(level==k)

cout<<root.data;

return;

}

fun(n->left);

if(level==k&&n!=start)

{ return;

}

else if(n==start)

{

level=0;

}

fun(n->rite)

if(n==start)

{ k=1;

}

}

}

thanks grt algo

- aaman.singh85 October 03, 2012we can do both in jus N-1 steps:

int large=0,sec_large=0;

for(i=0;i<N;i++)

if(arr[i]>large)

{ small=large;

large=arr[i];

}

else if(arr[i]<large&& arr[i]>sec_large)

sec_large=arr[i];

}

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window

Use one stack only , just keep track of level.Say when level%2==0 push left first then right, in else case do opposite , also make sure that u r printing out while adding to the stack.

- aaman.singh85 April 18, 2013