Why I got compile error with my Java solution?


  • 0
    S

    My Java solution is exactly the same as Binary Tree Level Order Traversal except with the difference that I am trying to use the method addFirst(E e) on a Linked List. However I got compile error (I don't know why?)! Here is my solution:

    import java.util.LinkedList;
    
    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            List<List<Integer>> res = new LinkedList<List<Integer>>();
            if(root == null){
                return res;
            }
            List<TreeNode> level = new LinkedList<TreeNode>();
            level.add(root);
            while(!level.isEmpty()){
                List<TreeNode> nextLevel = new LinkedList<TreeNode>();
                List<Integer> currLevel = new LinkedList<Integer>();
                for(TreeNode node : level){
                    currLevel.add(node.val);
                    if(node.left != null)   nextLevel.add(node.left);
                    if(node.right != null)  nextLevel.add(node.right);
                }
                res.addFirst(currLevel);
                level = nextLevel;
            }
            return res;
        }
    }
    

    Here is the error message:

    Compile Error Line 28: error: cannot find symbol: method
    addFirst(List<Integer>)
    Any ideas would be very helpful.


Log in to reply
 

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