top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to print all right nodes of tree?

+6 votes
381 views
The input tree is as shown below
            40
            / \
        20      60
        / \       \
    10      30      80
      \            /  \ 
       15        70    90

Output: 15 30 60 80 90
posted Nov 23, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes
void printRight(struct node* root)
{
    if (root == null) 
       return;

    if (root->left)
       printRight(root->left);

    if (root->right)
    {
       cout >> root->right.data();
       printRight(root->right);
    }
}
answer Nov 23, 2013 by Jai Prakash
0 votes
void print_right(Node *root){
   if(!root)
         return;

 if(root->right)
        printf("%d",root->right->data);

print_right(root->left);
print_right(root->right);
}
answer Dec 17, 2016 by anonymous
Similar Questions
+5 votes
      40
      /\
     20 60
     /\  \
   10 30  80
      /   /\
     25  70 90
           \
           75

longest path 
25 30 20 40 60 80 70 75 
Answer
    25 ----> 75
+7 votes

Print cousins of a given node (Not sibling) ??

+7 votes
     50
    /  \
   20   30
   / \
  70  80
 /  \   \
10   40  60        

printing the border elements anticlockwise direction:
->50->20->70->10->40->60->30

...