不兼容的类型:ArrayList <arraylist <integer>&gt;无法转换为List <list <integer>&gt;

时间:2015-10-01 16:51:56

标签: java list arraylist linked-list

我正在做一个Leetcode问题&#34; Binary Tree Zigzag Level Order Traversal&#34;用java。 我收到了编译错误

  

&#34;不兼容的类型:ArrayList&lt; ArrayList&lt;整数&GT;&GT;不可能是   转换为List&lt;列表&lt;整数&GT;&GT;&#34;

任何人都可以告诉我错在哪里以及如何更改代码? 非常感谢你!

以下是我的代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

        if (root == null) return result;

        Stack<TreeNode> currLevel = new Stack<TreeNode>();
        Stack<TreeNode> nextLevel = new Stack<TreeNode>();
        Stack<TreeNode> tmp;

        currLevel.add(root);
        boolean normalOrder = true;

        while(!currLevel.isEmpty()) {
            ArrayList<Integer> currLevelResult = new ArrayList<Integer>();
            while(!currLevel.isEmpty()) {
                TreeNode node = currLevel.pop();
                currLevelResult.add(node.val);
                if (normalOrder) {
                    if(node.left != null) nextLevel.add(node.left);
                    if(node.right != null) nextLevel.add(node.right);
                }
                else {
                    if(node.right != null) nextLevel.add(node.right);
                    if(node.left != null) nextLevel.add(node.left);
                }
            }
            result.add(currLevelResult);
            tmp = currLevel;
            currLevel = nextLevel;
            nextLevel = tmp;
            normalOrder = !normalOrder;
        }
        return result;
    }
}

0 个答案:

没有答案