Given a Binary Tree, write a function to check whether the given Binary Tree is Complete Binary Tree or not. A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. See following examples.
用bfs一层一层看,用变量记录是否能有child。
publicbooleanisComplete(TreeNode root) {if (root ==null) {returntrue; }boolean mustNotHasChild =false;Queue<TreeNode> queue =newLinkedList<>();queue.offer(root);while (!queue.isEmpty()) {TreeNode cur =queue.poll();// if you must not have child is true, and the node has child, return false;if (mustNotHasChild) {if (cur.left!=null||cur.right!=null) {returnfalse; } }// if left child & right child are both here, you can put them in queue for future processingif (cur.left!=null&&cur.right!=null) {queue.offer(cur.left);queue.offer(cur.right); } elseif (cur.left!=null&&cur.right==null) {// if right child is null, means next level you can't have childqueue.offer(cur.left); mustNotHasChild =true; } elseif (cur.left==null&&cur.right!=null) {// if left is null & right is not null, this is not a complete binary tree, so return false;returnfalse; } else {// last level, so mustNotHasChild =true; } }returntrue;}