leetcode 1609 奇偶树

    public boolean isEvenOddTree(TreeNode root) {
     
        if (root == null ) return true;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        boolean res = true;
        boolean level = true;
        flag : while (!queue.isEmpty()){
     
            int length = queue.size();
            TreeNode item = null;
            for (int i=0; i < length;i++){
     
                TreeNode node = queue.poll();
                if (node.left != null) queue.add(node.left);
                if (node.right != null) queue.add(node.right);
                if (level) {
     
                    if (node.val % 2 == 0) {
     
                        res = false;
                        break flag;
                    }
                    if (item == null){
     
                        item = node;
                    }else if (node.val <= item.val){
     
                        res = false;
                        break flag;

                    }else {
     
                        item = node;
                    }
                }else {
     
                    if (node.val % 2 == 1) {
     
                        res = false;
                        break flag;
                    }
                    if (item == null){
     
                        item = node;
                    }else if (node.val >= item.val){
     
                        res = false;
                        break flag;
                    }else {
     
                        item = node;
                    }
                }
            }
            level = !level;
        }
        return res;
    }

你可能感兴趣的:(leetcode,leetcode,队列)