clllll  

完全二叉树定义: 每一层都是满的,最后一层如果不满,也是从左到右依次排列

宽度优先遍历

1) 任一节点,有右孩子,没左孩子, return false;
2) 在1不违规的情况下,遇到第一个左右俩孩子不双全的情况。接下来所有的节点必须是叶节点

public static boolean isCBT(Node head) {
    if (head == null) {
        return true;
    }
    LinkedList<Node> queue = new LinkedList<>();
    Node cur = null;
    Node left = null;
    Node right = null;
    // 当遇到第一个左节点 或者 右节点 为null,就赋值为true;
    // 如果剩下的节点 还有不是 叶子节点(还有左节点,右节点)就返回false.
    boolean first_leaf = false;

    queue.add(head);
    while (!queue.isEmpty()) {
        cur = queue.poll();

        left = cur.left;
        right = cur.right;

        if (left == null && right != null) {
            // 如果碰到 左孩子为空,右孩子不为空,直接false
            return false;
        }
        if (first_leaf && (left != null || right != null)) {
            // 如果first_leaf为true,已经碰到第一个左右孩子为空的节点了。
            // 如果还有 node的左右孩子不为空,不为叶子节点,就return false
            return false;
        }

        if (left != null) {
            queue.add(left);
        }
        if (right != null) {
            queue.add(right);
        }

        if (left == null || right == null) {
            // 碰到第一个左孩子 或者右孩子 为空。
            first_leaf = true;
        }

    }
  return true;
}

满二叉树:

最大深度:L 节点数 N
必须满足 : N = 2^L - 1

树形DP

  • 子树收集的信息
public static class FullType{
        public int height; //当前 节点的高度
        public int nodes; //当前 节点 树 的所有node 个数
        public FullType(int height, int nodes){

            this.height = height;
            this.nodes = nodes;
        }
    }
  • 递归处理
public static FullType processFullBT(Node node){
    if(node == null){
        //base case
        return new FullType(0, 0);
    }

    // 处理左子树
    FullType leftData = processFullBT(node.left);
    // 处理右子树
    FullType rightData = processFullBT(node.right);

    // 处理信息
    int height = Math.max(leftData.height, rightData.height) + 1;
    int nodes = leftData.nodes + rightData.nodes + 1;

    return new FullType(height, nodes);

}
  • 调用递归
public static boolean isFullBT(Node head){
    FullType result = processFullBT(head);
    return result.nodes == (1<< result.height - 1);
    
}

平衡二叉树

对于任何一个子树,左树的高度和右树的高度差不超过1
左子树是平衡二叉树, 右子树是平衡二叉树
|左高 - 右高| <= 1
需要子树的高度 和 是否为平衡二叉树 俩个信息

  • 设置返回值 类
public static class ReturnType {

    public boolean isBalanced; //是否是平衡树
    public int height; // 树的高度

    public ReturnType(boolean isBalanced, int height){
        this.isBalanced = isBalanced;
        this.height = height;
    }
}
  • 递归处理
public static ReturnType processIsBalanced(Node node){
    if(node == null){
        // base case 
        return new ReturnType(true, 0);

    }

    // 获取左子树的信息
    ReturnType leftData = processIsBalanced(node.left);

    //获取右子树的信息
    ReturnType rightData = processIsBalanced(node.right);


    // 得到当前节点的高度
    int height = Math.max(leftData.height, rightData.height) + 1 ;
    // 判断当前节点是否符合 平衡树,
    // 三个条件,左子树为平衡二叉树,右子树为平衡二叉树, 左右子树的高度差小于等于 1 
    boolean isBalanced = leftData.isBalanced && rightData.isBalanced &&
                            Math.abs(leftData.height - rightData.height) <=1; 
    
    // 把当前节点的信息返回去
    return new ReturnType(isBalanced, height);
}
  • 调用递归
 public static boolean isBalanced(Node head){
        return processIsBalanced(head).isBalanced;
    }
posted on 2022-05-06 23:38  llcl  阅读(458)  评论(0)    收藏  举报