0
点赞
收藏
分享

微信扫一扫

面试必刷TOP101:35、判断是不是完全二叉树

Greatiga 2023-11-28 阅读 42

题目

面试必刷TOP101:35、判断是不是完全二叉树_Java

面试必刷TOP101:35、判断是不是完全二叉树_Java_02

题解

    public boolean isCompleteTree (TreeNode root) {
        // write code here
        if(root == null) return true;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        boolean isHasLeft = true;
        while(!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            if(poll == null) isHasLeft = false;
            else{
                if(isHasLeft == false) return isHasLeft;
                queue.add(poll.left);
                queue.add(poll.right);
            }
        }
        return true;
    }

举报

相关推荐

0 条评论