我正在做一个Leetcode问题" Binary Tree Zigzag Level Order Traversal"用java。 我收到了编译错误
"不兼容的类型:ArrayList< ArrayList<整数>>不可能是 转换为List<列表<整数>>"
任何人都可以告诉我错在哪里以及如何更改代码? 非常感谢你!
以下是我的代码:
/**
* 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;
}
}