Why my java code always indicate compile error:Line 30: error: reached end of file while parsing


  • 0
    S
    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Iterator;
    
    public class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            LinkedList<TreeNode> list = new LinkedList<TreeNode>();
            list.add(root);
            Iterator<TreeNode> iter = list.iterator();
            while(iter.hasNext()){
            	TreeNode temp = list.remove();
            	if(temp!=null){
                	result.add(temp.val);
                    list.add(temp.left);
                    list.add(temp.right);
            	}
            }
    		return result;
    }

  • 2
    S
    public class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            LinkedList<TreeNode> list = new LinkedList<TreeNode>();
            list.add(root);
            Iterator<TreeNode> iter = list.iterator();
            while(iter.hasNext()){
                TreeNode temp = list.remove();
                if(temp!=null){
                    result.add(temp.val);
                    list.add(temp.left);
                    list.add(temp.right);
                }
            }
            return result;
        }  // missing this for public List<Integer> preorderTraversal(TreeNode root)
    }

Log in to reply
 

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