Got different answer from OJ and my eclipse


  • 0
    Y

    import java.util.LinkedList;
    import java.util.List;

    public class Solution {
    static List<Integer> a = new LinkedList<Integer>();
    public static List<Integer> preorderTraversal(TreeNode root) {
    //if(root == null)
    //return a;
    a.add(root.val);
    if(root.left != null)
    preorderTraversal(root.left);
    if(root.right != null)
    preorderTraversal(root.right);
    return a;
    }
    }

    As for input{1,2},my eclipse return right answer but OJ throws a wrong answer with my code{1,1,2}.
    Very weird.


  • 0
    S

    Please correct your code format.


  • 0

    Please refer to the FAQ https://oj.leetcode.com/faq/ page for more detail. Especially the question "Why does my code produce a different output compared to my local environment?"


Log in to reply
 

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