6、完全二叉树
1、定义
-
[1、任意节点有右无左false
-
在 [1 不违规的条件下,如果遇到第一个左右子都不全—且下一个不为叶节点—返回false
-
采用宽度优先遍历
-
//判断是否为完全二叉树 public boolean isCbt(TreeNode treeNode){ if (treeNode == null){ return true; } Queue<TreeNode> queue = new LinkedList<>(); TreeNode l = null; TreeNode r = null; boolean flag = false; queue.add(treeNode); while (!queue.isEmpty()){ treeNode = queue.poll(); l = treeNode.left; r = treeNode.right; if (((flag && (l !=null || r !=null))) || (l == null && r != null)){ //条件1---当遇到左右不全有的节点时,切下一个节点有左或者有右孩子时返回false //条件2---当有右无左直接false return false; } if (l !=null){ queue.add(l); } if (r != null){ queue.add(r); } if (l == null || r == null){ //遇到左右不全有的节点时开关打开 flag = true; } } return true; }