Title Description:
Given a binary tree, determine whether it is a complete binary tree.
The definition of complete binary tree in Baidu Encyclopedia is as follows:
If the depth of the binary tree is h, the number of nodes in all layers (1-h-1) except the H layer reaches the maximum, and all nodes in the H layer are continuously concentrated on the left, which is the complete binary tree. (Note: layer h may contain 1-2h nodes.)
Example 1:
Input: [1,2,3,4,5,6]
Output: true
Explanation: each layer before the last layer is full (that is, two layers with node values of and ), and all nodes in the last layer () are left as far as possible.
Example 2:
Input: [1,2,3,4,5,null,7]
Output: false
Explanation: nodes with a value of 7 are not as far to the left as possible.
Tips:
There will be 1 to 100 nodes in the tree.
Source: LeetCode
Link: https://leetcode-cn.com/problems/check-completion-of-a-binary-tree
Copyright belongs to the network. For commercial reprint, please contact the official authorization. For non-commercial reprint, please indicate the source.
At the beginning, my idea was rather complicated. First, I calculated the height of the binary tree, and then we traversed layer by layer. When we met the penultimate layer, we need to pay attention to it
Redundancy in code
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isCompleteTree(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); int dep = getdep(root) - 1; queue.offer(root); int curdep = 0; while (!queue.isEmpty()){ if(curdep == dep){ return true; } int size = queue.size(); if(curdep < dep - 1){ for (int i = 0; i < size; i++) { TreeNode poll = queue.poll(); if(poll.left == null || poll.right == null){ return false; } queue.offer(poll.left); queue.offer(poll.right); } // curdep ++; }else{ boolean flag = true; for (int i = 0; i < size; i++) { TreeNode poll = queue.poll(); if(poll.right != null && poll.left == null){ return false; } // There's a if((poll.left != null || poll.right != null) && !flag){ return false; } if((poll.left != null && poll.right == null) || (poll.left == null && poll.right == null)){ flag = false; } if(poll.left != null){ queue.offer(poll.left); } if(poll.right != null){ queue.offer(poll.right); } } // curdep ++; } curdep ++; } return false; } public int getdep(TreeNode root){ if(root == null){ return 0; } int left = getdep(root.left); int right = getdep(root.right); return Math.max(left,right) + 1; } }
See what others have achieved, simple and clear
In fact, it's very simple. As long as you traverse to the null node, see if there is any. If there is any, return false. Otherwise, it's true
class Solution { /** * Span traversal binary tree, when there is a null value, stop traversing, if there is no node traversed at this time, it means that the tree is not a complete binary tree. */ public boolean isCompleteTree(TreeNode root) { LinkedList<TreeNode> q = new LinkedList<>(); TreeNode cur; q.addLast(root); while ((cur = q.removeFirst()) != null) { q.addLast(cur.left); q.addLast(cur.right); } while (!q.isEmpty()) { if (q.removeLast() != null) { return false; } } return true; } }wenbaoxie 797 original articles published, praised 23, visited 30000+ His message board follow