Binary Tree Right Side View – Leetcode Solution

In this post, we are going to solve the 199. Binary Tree Right Side View problem of Leetcode. This problem 199. Binary Tree Right Side View is a Leetcode medium level problem. Let’s see the code, 199. Binary Tree Right Side View – Leetcode Solution.

Problem

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 :

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

Constraints

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

Now, let’s see the code of 199. Binary Tree Right Side View – Leetcode Solution.

Binary Tree Right Side View – Leetcode Solution

199. Binary Tree Right Side View – Solution in Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> rightSideView(TreeNode root) {
         if(root == null) return new ArrayList<>();
         Queue<TreeNode> q = new LinkedList<>();
         ArrayList<Integer> ans = new ArrayList<>();
         q.add(root);

         while(!q.isEmpty()){
              int size = q.size();
              ans.add(q.peek().val);
              while(size-- > 0){
                  TreeNode parNode = q.poll();
                  if(parNode.right != null) q.add(parNode.right);
                  if(parNode.left != null) q.add(parNode.left);
              }
          }
             return ans;
        }
}

199. Binary Tree Right Side View – Solution in C++

/**
 * 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:
    vector<int> rightSideView(TreeNode *root) {
        queue<TreeNode*>mQ;
        vector<int> ret;
        if(!root)return ret;
        mQ.push(root);
        while(!mQ.empty()){
            ret.push_back(mQ.back()->val);
            for(int i=mQ.size();i>0;i--){
                TreeNode *tn=mQ.front();
                mQ.pop();
                if(tn->left)mQ.push(tn->left);
                if(tn->right)mQ.push(tn->right);
            }
        }
        return ret;
    }
};

199. Binary Tree Right Side View – Solution in Python

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def rightSideView(self, root):
        deque = collections.deque()
        if root:
            deque.append(root)
        res = []
        while deque:
            size, val = len(deque), 0
            for _ in range(size):
                node = deque.popleft()
                val = node.val # store last value in each level
                if node.left:
                    deque.append(node.left)
                if node.right:
                    deque.append(node.right)
            res.append(val)
        return res
        

Note: This problem 199. Binary Tree Right Side View is generated by Leetcode but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *