My simple accepted java solution with ArrayList


  • 0
    D
    public class Solution {
        public List<Integer> rightSideView(TreeNode root) {
             List<Integer> rightView = new ArrayList<Integer>();
             List<TreeNode> q = new ArrayList<TreeNode>();
             if(root == null) return rightView;
             q.add(root);
             while(!q.isEmpty()) {
                 int levelNum = q.size();
                    rightView.add(q.get(q.size() - 1).val); 
                
                 for(int i = 0; i < levelNum;i++) {
                     if(q.get(0).left != null) q.add(q.get(0).left);
                     if(q.get(0).right != null) q.add(q.get(0).right);
                     q.remove(0);
                 }
                 
             }
             return rightView;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.