Acc Java Solution


  • 0
    A
    public class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        ArrayList<Integer> out = new ArrayList<>();
        if (root == null) return out;
        LinkedList<TreeNode> lList = new LinkedList<>();
        lList.add(root);
        while (lList.size()!=0) {
            int size = lList.size();
            out.add (lList.peekLast().val);
            for (int i=0; i < size; i++) {
                TreeNode node =  lList.poll();
                if (node.left != null) lList.addLast(node.left);
                if (node.right != null) lList.addLast(node.right);
             }
        }
        return out;
    }

Log in to reply
 

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