Ans to *Boundary traversal of a tree. Write the code.

*1) you need to for preoder oder for left most tree with flag and post
order traversal for right most tree with flag.
2) the role of flag would be to decide wther to print or not like in case
of left subtree ,flag would be tree as u knw that
    in preorder traversal left element would be boundary and once u go
right make it false .. same for right subtree:

Code snippet would look like this :

void printleft (struct node *root, bool flag) {
  if (!root) return;
  if (flag || (!root->left && !root->right)) {
    cout<< root->data;
  }
  printleft(root->left, true);
  printleft(root->right, false);
}

this is preorder for left subtree , do post order for right subtree like :

void printright (struct node *root, bool flag) {
  if (!root) return;
  printright(root->left, false);
  printright(root->right, true);
  if (flag || (!root->left && !root->right)) cout<< root->data;

}
*
*let me knw if anything seems confusing.
On Sun, Mar 10, 2013 at 4:59 PM, Nishant Pandey <
nishant.bits.me...@gmail.com> wrote:

> i have few questions regarding ur problems pratik :
>
> 1) A tree with only parent pointer, how to find LCA?
> Doubt : do u mean only root of the tree is given , i am assuming root
> along with two nodes address whose lca need to
>          find too is given , i am right ?
>
> 2) Find top k searched elements from a continuous stream of data.
> Doubt : what do u mean by top k search element.... can u please explain
> little bit with exmple.
>
>
> I would love to post solution for you provided clear my doubts ....
> may i knw which Amazon's round questions are they ?
>
>
> On Mon, Feb 18, 2013 at 1:28 AM, Tushar <tushicom...@gmail.com> wrote:
>
>> It looks as if you have just pasted some Amazon interview questions on
>> this forum.
>> These are pretty common questions.
>> Try to come up with your own answers.
>> Do some research on google and previous posts on this forum. You'll get
>> answers to all of them.
>>
>> If you have some idea and want to discuss that, then post different posts
>> for each questions as arun recommended along with your understanding of the
>> question and your approach.
>>
>> All the best
>>
>>
>> On Saturday, 9 February 2013 14:45:35 UTC+5:30, Pratik Mehta wrote:
>>>
>>> Hi All,
>>> I need ur help in solving few questions.
>>>
>>> Would you please help me out *BY GIVING THE ALGORITHM AND THE LOGIC
>>> BEHIND IT and it's DEEP EXPLANATION IF POSSIBLE?*
>>>
>>>
>>> *
>>> *
>>>
>>>  *a. Kadane’s Algo.*
>>>
>>>  *
>>> *
>>>
>>> *b. Linked-list intersection point.*
>>>
>>> *
>>> [A tree with only parent pointer, how to find LCA?]
>>> *
>>>
>>> *
>>>
>>> *
>>>
>>> *
>>> c. Design a stack which can perform findMax in O(1).
>>> *
>>>
>>> *
>>>
>>> *
>>>
>>> *d. Set of stocks for each day have been given. Need to find the days
>>> on which I buy and sell share to earn max profit, alongwith finding the max
>>> profit.*
>>>
>>>
>>> *
>>> e. Find top k searched elements from a continuous stream of data.
>>> *
>>>
>>> *
>>>
>>> *
>>>
>>> *f. Given a linked-list and 2 integers k & m. Reverse the linked-list
>>> till k elements and then traverse till m elements and repeat.*
>>>
>>> *Write production quality code.*
>>>
>>> *
>>> *
>>>
>>> *g. An array of elements have been given. Find for each element, first
>>> max element to its right.*
>>>
>>> *
>>> *
>>>
>>> *h. Boundary traversal of a tree. Write the code.*
>>>
>>>
>>> Please help me out...
>>>
>>> Thanks in advance...
>>>
>>> Thanks & Regards,
>>> Pratik Mayur Mehta.
>>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to algogeeks+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to