Java verifies whether a tree is a complete binary tree

Complete binary tree

  If the depth of the binary tree is k, the number of nodes in the other layers (1~k-1) has reached the maximum except for the kth layer, and all the nodes of the kth layer are continuously concentrated on the leftmost side , which is complete Binary tree.

 

 

Determine whether the binary tree is a complete binary tree. The definition of a complete binary tree is that the first n-1 levels are full, and if there is a vacancy in the nth level, it is missing on the right, that is, the rightmost node of the nth level, and its left side is full and the right side is empty .

Taking a 3-level binary tree as an example, the following situation is a complete binary tree:

 

We can do the question according to the meaning of the question. We can use the hierarchical traversal method. When judging a specific node, we can have the following judgment basis:
(1). If the left subtree of this node is null, If the right subtree is not null, it must not be a complete binary tree.
(2) . If the left and right subtrees of this node are all null (this does not need to determine whether the current layer or the next layer can no longer contain nodes containing left and right subtrees) , or the left subtree of this node is not null but the right subtree is null, the nodes containing left and right subtrees can no longer appear in the current layer or the next layer.
(3). If the left and right subtrees of the current node are not null, observe the next node.

public boolean isCompleteTree(TreeNode root){
        
        if(root == null){
            return false;
        }
        
        // 1. 先对树进行层序遍历
        // 如果这个标记为 false, 说明还是处在第一阶段
        // 如果这个标记为 true , 接下来的节点就不能有子树
        // 也就是第二阶段开始了
        int flg = 1;//第一阶段:1 第二阶段:2
        
        LinkedList<TreeNode> queue = new LinkedList();
        
        queue.add(root);
        
        while(!queue.isEmpty()){
            TreeNode temp = queue.poll();
            
            if(flg == 1){
                
                // 合格的状态, 继续往下遍历.
                // 就把左右子树加入队列就行了
                if(temp.left != null && temp.right != null){
                    queue.offer(temp.left);
                    queue.offer(temp.right);
                }else if(temp.left == null && temp.right != null){
                    // 这种情况铁定不是完全二叉树
                    return false;
                }else if(temp.left != null && temp.right == null){
                    // 这是临界状态, 开启第二阶段
                    queue.offer(temp.left);
                    //开启第二阶段
                    flg = 2;
                }else{
                    // 左右子树都为空, 开启第二阶段
                    flg = 2;
                }
            }else{
                //开启第二阶段
                // 第二阶段要求节点必须没有子树. 只要子树存在, 就不是完全二叉树
                if(temp.left != null || temp.right != null){
                    return false;
                }
            }
        }
        
        return true;
    }

. If the left and right subtree of this node are null (this need not determine the current layer or the lower layer can not be left subtree node containing a present) wording:

This is easier to understand: 

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    
    //是否是儿茶搜索树
    boolean isTwoSearchFlg = true;
    
    /**
     * 
     * @param root TreeNode类 the root
     * @return bool布尔型一维数组
     */
    public boolean[] judgeIt (TreeNode root) {
        boolean[] result = new boolean[2];
        istwoSearchTree(root);
        result[0] = isTwoSearchFlg;
        
        result[1] = isFullTree(root);
        
        return result;
    }
    
    int preTreeNodeVal = Integer.MIN_VALUE;
    public void istwoSearchTree(TreeNode root){
       
        if(root == null || !isTwoSearchFlg){
            return;
        }
        
        istwoSearchTree(root.left);
        if(root.val > preTreeNodeVal){
            preTreeNodeVal = root.val;
        }else{
            isTwoSearchFlg = false;
            return;
        }
        istwoSearchTree(root.right);
    }
    
    
    
    public boolean isFullTree(TreeNode root){
        
        if(root == null){
            return false;
        }
        
        int condition = 1;
        
        LinkedList<TreeNode> queue = new LinkedList();
        queue.offer(root);
            
        while(!queue.isEmpty()){
            TreeNode temp = queue.poll();
            
            if(1 == condition){
                if(temp.left!=null && temp.right!=null){
                    queue.offer(temp.left);
                    queue.offer(temp.right);
                }else if(temp.left == null && temp.right != null){
                    return false;
                }else if(temp.left!=null && temp.right == null){
                    condition = 2;
                    queue.offer(temp.left);
                }
            }else{
                if(temp.left!=null || temp.right != null){
                    return false;
                }
            }
        }
        
        return true;
    }
}

 

Guess you like

Origin blog.csdn.net/luzhensmart/article/details/112639981
Recommended