A very weird mistake, who can help?


  • 0
    L

    response is: java.util.ConcurrentModificationException for the "while loop"

    public class Solution {
    public int minDepth(TreeNode root) {
        if(root == null) return 0;
        int height = 0;
        List<TreeNode> level = new ArrayList<>();
        level.add(root);
        List<TreeNode> nextLevel = new ArrayList<>();
        while(!level.isEmpty() && level.get(0)!=null) {
            for(TreeNode node : level) {
                if(node.left != null && node.right != null) {
                    nextLevel.add(node.left);
                    nextLevel.add(node.right);
                }
                else return height++;
            }
            height++;
            level = nextLevel;
        }
        return height;
    }
    

    }


Log in to reply
 

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