• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Right view of Binary tree

July 24, 2016 by Dhaval Dave

We are given a Binary Tree, Print the Right view of Binary tree,
Right view of binary tree is  : List of all nodes which are visible If you look at Binary tree from right side.
For example,

Right view of following tree is a, c, g, h  

          a
       /     \
     b        c
   /   \     /  \
  d     e   f    g
                  \
                   h

Hint for logic :

First Simple Method for Right view of Binary Tree:

  • Tra­verse the Binary tree from right to left
  • Print the first “node->data” you encounter
  • Take two vari­ables , currentLevel=0 and nextLevel=1
  • when you change level , change the cur­rentLevel = nextLevel
  • Print node->data only when cur­rent level<nextLevel to print only the first element in the right.
  • For rest of the nodes on the the level cur­rentLevel and nextLevel are equal so it wont print.

Second Method : Level Order Traversal for Right view of Binary Tree:

If you Print a Tree in breadth first search manner, then it will print
a,   b,  c,  d,  e,  f,  g,  h, now the list of nodes we visit the last in each breadth  is required list.

Lets Code given problem

Recursive way

 

void rightViewUtil(struct Node *node, int bredth, int *m_l){
    if (node==NULL) return;
    // Check If current node is the last one of its level/breadth
    
   if (*m_l < bredth){
       printf("%d\t", node->data);
       *m_l = bredth;
    }

   // Recursion for right subtree and then left subtree
   rightViewUtil(node->right, bredth+1, m_l);
   rightViewUtil(node->left, bredth+1, m_l);
}

void rightView(struct Node *node){
    int max_level = 0;
    rightViewUtil(node, 1, &max_level);
}

Time Complexity : O(n)
Space Complexity : O(1) if you don’t consider stack space utilized by Recursion.

 

 

Nissan qashqai acenta 1.6 dci, Swot ford fiesta, Volkswagen golf variant diesel 2009

Similar Articles

Filed Under: Amazon Interview Question, Flipkart Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Binary Tree, tree

Reader Interactions

Trackbacks

  1. VMWare SDEII Interview | GoHired says:
    July 24, 2016 at 5:33 pm

    […] Right View of a Binary Tree, its edge cases and failing cases to handle in code Solution : http://gohired.in/2016/07/24/right-view-of-binary-tree/ […]

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Generate largest number arranging a no. of given non negative integer numbers

Find min element in Sorted Rotated Array (With Duplicates)

Daughter’s Age VeryGood Puzzle

Handle duplicates in Binary Search Tree

Amazon Interview Experience – SDE Chennai

Amazon Interview On-Campus For Internship – 1

Find Pythagorean Triplets in an array in O(N)

1014 Practice Question of New GRE – Princeton

Connect n ropes with minimum cost

Word Break Problem

Binary Tree in Java

Binary Tree in Java

CodeChef Code SGARDEN

Sequence Finder Dynamic Programming

Templates in C++

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Flipkart Set 1 On Campus with Answers

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Check if an array has duplicate numbers in O(n) time and O(1) space

Adobe Interview Questions 8 month Exp

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Best Java Book | Top Java Programming Book for Beginners

Code Chef PRGIFT Solution

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Memory Efficient LinkedList

Maximum size of square sub matrix with all 1’s in a binary matrix

Find Percentage of Words matching in Two Strings

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Given a float number convert it into the string WITHOUT using any inbuilt Function

Knight Tour Problem (Graph – Breadth First Search)

Copyright © 2025 · Genesis Framework · WordPress · Log in