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.

&nbsp;
Example 1:

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


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [0, 100].
	-100 &lt;= Node.val &lt;= 100


# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
    def rightSideView(self, root: Optional[TreeNode]) -> List[int]:

        def dfs(root, d):

            if not root: return

            if self.maxi < d:
                self.res.append(root.val)
                self.maxi = d

            dfs(root.right, d+1)
            dfs(root.left, d+1)

        self.res, self.maxi = [], 0
        dfs(root, 1)
        return self.res

        # An Upvote will be encouraging
class Solution {
    // Utility Function of RightSideView
    public void rightView(TreeNode curr, List<Integer> list, int level) {
        // if, current is null, return
        if(curr == null) {
            return;
        }
        
        // if, level = list size
        // add current val to list
        if(level == list.size()) {
            list.add(curr.val);
        }
        
        // recursive call for right side view
        rightView(curr.right, list, level + 1);
        // recursive call for left side view
        rightView(curr.left, list, level + 1);
    }
    
    // Binary Tree Right Side View Function
    public List<Integer> rightSideView(TreeNode root) {
        // create a list
        List<Integer> result = new ArrayList<>();
        // call right view function
        rightView(root, result, 0);
        return result;
    }
}

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

// Time & Space Complexity -
/*
Time - O(n)
Space - O(h) h = height of binary tree
*/
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void solve(TreeNode* root, vector<int> &ans, int level)
    {
        // base case
        if (root == NULL)
        {
            return ; 
        }

        // we entered a new level 
        if (level == ans.size())
        {
            ans.push_back(root -> val) ; 
        }

        solve(root -> right, ans, level + 1) ; 
        solve(root -> left, ans, level + 1 ) ; 
    }
    vector<int> rightSideView(TreeNode* root) {
        vector<int> ans ; 
        int level = 0 ; 

        solve(root, ans, level) ;
        
        return ans ; 
    }
};
var rightSideView = function(root) {
    if(!root) return [];
    let ans = [];
    let queue = [root];
	
    while(queue.length > 0){
        let queueLength = queue.length;
		
		// As we are giving priority to right node, 
		// first node will always be the one that we want in output 
        ans.push(queue[0].val);
		
        for(let i = 0;i<queueLength;i++){
			// Ignore TC of queue.shift (assuming we have queue implementation with O(1) TC)
            let current = queue.shift();
			
			// Always give priority to right node
            if(current.right) queue.push(current.right);
            if(current.left) queue.push(current.left);
        }
    }
    return ans;
};
Binary Tree Right Side View