Binary Tree Right Side View LeetCode Solution

Last updated on January 7th, 2025 at 03:15 am

Here, we see the Binary Tree Right Side View LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.

List of all LeetCode Solution

Topics

Breadth-First-Search, Depth-First-Search, Tree

Companies

Amazon

Level of Question

Medium

Binary Tree Right Side View LeetCode Solution

Binary Tree Right Side View LeetCode Solution

1. 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: []

2. Coding Pattern Used in Solution

The coding pattern used in all the provided implementations is Tree Traversal. Specifically:

  • The C++ and Python implementations use Tree Breadth-First Search (BFS) to traverse the tree level by level.
  • The Java and JavaScript implementations use Tree Depth-First Search (DFS) to traverse the tree recursively, prioritizing the right subtree first.

3. Code Implementation in Different Languages

3.1 Binary Tree Right Side View 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());
        }
    }
};

3.2 Binary Tree Right Side View 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);
        
    }
}

3.3 Binary Tree Right Side View 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);
    }
};

3.4 Binary Tree Right Side View 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

4. Time and Space Complexity

Time ComplexitySpace Complexity
C++O(N)O(W)
JavaO(N)O(H)
JavaScriptO(N)O(H)
PythonO(N)O(W)

Here,
N: Total number of nodes in the tree.
H: Height of the tree (log(N) for balanced trees, N for skewed trees).
W: Maximum width of the tree (number of nodes at the widest level).

Scroll to Top