
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Binary Tree Right Side View in C++
Suppose we have a binary tree, if we see the tree from right side, then we can see some elements of it. we have to display those elements. So if the tree is like −
To solve this, we will follow these steps −
- We will create one helping method for dfs. This will take tree_node, an array to hold answers, and level. The level is initially 0. The dfs will work like below −
- if node is null, then return
- if level = length of the answer array, then insert value of node into the ans array
- dfs(right of the node, ans, level + 1)
- dfs(left of the node, ans, level + 1)
- From the main function call the dfs() using the root of the tree and one blank array, the level is initially 0, so this will be dfs(root, ans)
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; void print_vector(vector<int> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = right = NULL; } }; void insert(TreeNode **root, int val){ queue<TreeNode*> q; q.push(*root); while(q.size()){ TreeNode *temp = q.front(); q.pop(); if(!temp->left){ if(val != NULL) temp->left = new TreeNode(val); else temp->left = new TreeNode(0); return; } else { q.push(temp->left); } if(!temp->right){ if(val != NULL) temp->right = new TreeNode(val); else temp->right = new TreeNode(0); return; } else { q.push(temp->right); } } } TreeNode *make_tree(vector<int> v){ TreeNode *root = new TreeNode(v[0]); for(int i = 1; i<v.size(); i++){ insert(&root, v[i]); } return root; } class Solution { public: void dfs(TreeNode* node, vector <int>& ans, int level = 0){ if(!node) return; if(level == ans.size())ans.push_back(node->val); dfs(node->right, ans, level + 1); dfs(node->left, ans, level + 1); } vector<int> rightSideView(TreeNode* root) { vector <int> ans; dfs(root, ans); return ans; } }; main(){ vector<int> v = {1,2,3,NULL,5,NULL,4}; TreeNode *root = make_tree(v); Solution ob; print_vector(ob.rightSideView(root)); }
Input
[1,2,3,null,5,null,4]
Output
[1, 3, 4, ]
- Related Articles
- Print Right View of a Binary Tree in C language
- Program to find the left side view of a binary tree in C++
- Print Left View of a Binary Tree in C language
- Program to find top view of a binary tree in Python
- Program to print nodes in the Top View of Binary Tree using C++
- Find maximum among all right nodes in Binary Tree in C++
- Binary Tree to Binary Search Tree Conversion in C++
- Find right sibling of a binary tree with parent pointers in C++
- Find sum of all right leaves in a given Binary Tree in C++
- C++ Program to Perform Right Rotation on a Binary Search Tree
- Difference between Binary Tree and Binary Search Tree
- Binary Tree in Javascript
- Print all leaf nodes of a binary tree from right to left in C++
- Program to find sum of the right leaves of a binary tree in C++
- Print leaf nodes in binary tree from left to right using one stack in C++

Advertisements