Binary Tree Right Side View LeetCode Solution

Here, We see Binary Tree Right Side View LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Binary Tree Right Side View LeetCode Solution

Binary Tree Right Side View LeetCode Solution

Problem Statement

Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example 1:

tree

Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]

Example 2:
Input: root = [1,null,3]
Output: [1,3]

Example 3:
Input: root = []
Output: []

Binary Tree Right Side View LeetCode Solution C++

class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int>ans;
        queue<TreeNode*> q;
        if(root==NULL)
        return ans;
        q.push(root);
        while(1)
        {
            int size=q.size();
            if(size==0)
            return ans;
            vector<int> data;
            while(size--)
            {
                TreeNode* temp=q.front();
                q.pop();
                data.push_back(temp->val);
                if(temp->left!=NULL)
                q.push(temp->left);
                if(temp->right!=NULL)
                q.push(temp->right);
            }
            ans.push_back(data.back());
        }
    }
};Code language: PHP (php)

Binary Tree Right Side View LeetCode Solution Java

class Solution {
    int maxlevel = 0;
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> list  = new ArrayList<>();
        right(root,1,list);
        return list ;
    }
    void right(TreeNode root,int level,List<Integer> list){
        if(root==null){
            return ;
        }
        if(maxlevel<level){
            list.add(root.val);
            maxlevel=level;
        }
        right(root.right,level+1,list);
        right(root.left,level+1,list);
        
    }
}Code language: PHP (php)

Binary Tree Right Side View Solution JavaScript

var rightSideView = function(root) {
    if (!root) return [];
    let res = [];
    pre(root, 0);
    return res;
    function pre(node, h) {
        if (!node) return;
        res[h] = node.val;
        pre(node.left, h+1);
        pre(node.right, h+1);
    }
};Code language: JavaScript (javascript)

Binary Tree Right Side View Solution Python

class Solution(object):
    def rightSideView(self, root):
        queue = deque()
        if root is None:
            return []
        if root.left is None and root.right is None:
            return [root.val]
        result = []
        queue.append(root)
        while queue:
            child_queue = deque()
            prev = -1
            while queue:
                curr = queue.popleft()
                if curr.left is not None:
                    child_queue.append(curr.left)
                if curr.right is not None:
                    child_queue.append(curr.right)
                prev = curr
            result.append(prev.val)
            queue = child_queue
        return result
Scroll to Top