The traversal of the binary tree hierarchy and the judgment of whether a binary tree is a complete binary tree

Sequence traversal

We know that the traversal of the binary tree is divided intoDepth-first traversal and breadth-first traversal
Depth-first traversal is divided intoFirst-order traversal, middle-order traversal, post-order traversal
Breadth first traversal is Sequence traversal

As the name implies, the sequence traversal is a layer-by-layer traversal, from top to bottom, from left to right, as shown in the figure below: The result of the layer sequence traversal is: abcdefg
Insert picture description here

Ideas:

How to traverse the binary tree sequence? We first get the root node, then visit the left node, visit the right node...
We can put the nodes in the queue, and then put the left and right nodes into the queue respectively, and finally output in turn to get the sequence traversal.

Code:

public class TreeNode {
    
    
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int val) {
    
    
        this.val = val;
    }

    @Override
    public String toString() {
    
    
        return String.format("TreeNode{%c}", val);
    }
}
import java.util.LinkedList;
import java.util.Queue;
  
public class levelTraversal {
    
    
    public void level(TreeNode root) {
    
    
        if (root == null) {
    
    
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
    
    
            TreeNode node = queue.remove();
            System.out.println((char) node.val);
            if (node.left != null) {
    
    
                queue.add(node.left);
            }
            if (node.right != null) {
    
    
                queue.add(node.right);
            }
        }
    }
}
/**
 *
 */
public class Test {
    
    
    public static void main(String[] args) {
    
    
        TreeNode a = new TreeNode('a');
        TreeNode b = new TreeNode('b');
        TreeNode c = new TreeNode('c');
        TreeNode d = new TreeNode('d');
        TreeNode e = new TreeNode('e');
        TreeNode f = new TreeNode('f');
        TreeNode g = new TreeNode('g');
        TreeNode h = new TreeNode('h');

        a.left = b; a.right = c;
        b.left = d; b.right = e;
        c.left = f; c.right = g;
        e.right = h;

        levelTraversal levelTraversal=new levelTraversal();
        levelTraversal.level(a);
    }
}

result:
Insert picture description here

Determine whether a binary tree is a complete binary tree

The complete binary tree is derived from the full binary tree. For a binary tree with a depth of k and n nodes, if and only if all of its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k, it is a complete binary tree.
Full binary tree is a special kind of complete binary tree.
Insert picture description here
We can traverse the binary tree sequence (including empty nodes).
Complete binary tree result: abcdefg#####
Incomplete binary tree result: abcd##ef###
When null is encountered in the queue When, if the following is null, then it is a complete binary tree, otherwise it is not a complete binary tree;

Topic link:
Completeness test of binary tree
Insert picture description here

class Solution {
    
    
    public boolean isCompleteTree(TreeNode root) {
    
    
        if (root == null) {
    
    
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        boolean flag = false;
        while (!queue.isEmpty()) {
    
    
            TreeNode node = queue.remove();
            if (node == null) {
    
    
                flag = true;
                continue;
            }
            if (flag) {
    
    
                return false;
            }
            queue.add(node.left);
            queue.add(node.right);

        }
        return true;
    }
}

Guess you like

Origin blog.csdn.net/weixin_52142731/article/details/115271487