题解 | #判断是不是完全二叉树#
判断是不是完全二叉树
https://www.nowcoder.com/practice/8daa4dff9e36409abba2adbe413d6fae
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ public boolean isCompleteTree (TreeNode root) { Queue<TreeNode> qu = new LinkedList<>(); boolean flag1 = true; boolean flag2 = true; qu.offer(root); while (!qu.isEmpty()) { TreeNode top = qu.poll(); if (flag1&&flag2) { if (top.left != null) { qu.offer(top.left); if (top.right != null) { qu.offer(top.right); }else{ flag2=false; } } else { flag1 = false; if (top.right != null) { return false; } } }else{ if(top.left!=null||top.right!=null){ return false; } } } return true; } }