189 8069 5689

判断二叉树是否为满二叉树-创新互联

判断二叉树是否为满二叉树

作者:Grey

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、虚拟主机、营销软件、网站建设、城关网站维护、网站推广。

原文地址:

博客园:判断二叉树是否为满二叉树

:判断二叉树是否为满二叉树

满二叉树定义

一个二叉树,如果每一个层的结点数都达到大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是(2^k) -1,则它就是满二叉树。

方法1

使用公式,求二叉树的层数 k, 结点数 n,如果满足(2^k) -1 = n,则为满二叉树。

定义数据结构

public static class Info1 {public int height;
        public int nodes;

        public Info1(int h, int n) {height = h;
            nodes = n;
        }
    }

其中height表示二叉树的层数,nodes表示二叉树的结点个数。

定义递归函数

Info1 process1(Node head)

递归含义是:head 为头的二叉树的层数和点数都是多少,接下来就是 base case

即:head == null的时候,此时,height == 0nodes == 0

if (head == null) {return new Info1(0, 0);
        }

接下来是普遍情况

// 去左树上收集信息
Info1 leftInfo = process1(head.left);
// 去右树上收集信息
Info1 rightInfo = process1(head.right);
// 整合成 head 自己的信息
int height = Math.max(leftInfo.height, rightInfo.height) + 1;
int nodes = leftInfo.nodes + rightInfo.nodes + 1;
return new Info1(height, nodes);
方法2

定义如下数据结构

public static class Info2 {public boolean isFull;
        public int height;

        public Info2(boolean f, int h) {isFull = f;
            height = h;
        }
    }

其中isFull表示是否为满二叉树,height表示二叉树的高度。

定义了这个数据结构后,可以梳理可能性,如果以head为头的树要符合满二叉树。则需要同时满足下面三种情况

情况1:左树是满二叉树

情况2:右树是满二叉树;

情况3:左右树的高度一样。

定义递归函数

Info2 process2(Node head)

递归含义就是返回以head为头的二叉树Info2结构信息。

base case是

if (h == null) {return new Info2(true, 0);
        }

h == null默认是满二叉树,结点个数为0。

接下来是普遍情况,即去左右子树收集相关信息,整合成以h为头二叉树的信息。

// 去左子树收集相关信息
    Info2 leftInfo = process2(h.left);
    // 去右子树收集相关信息
    Info2 rightInfo = process2(h.right);
    // 整合成 h 自己的新
    boolean isFull = leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height;
    int height = Math.max(leftInfo.height, rightInfo.height) + 1;
    return new Info2(isFull, height);

方法1 和 方法2 的时间复杂度都是O(n),即经过一次后序遍历的时间复杂度。

两种解法的完整代码(含测试代码)如下

public class Code_IsFull {public static class Node {public int value;
        public Node left;
        public Node right;

        public Node(int data) {this.value = data;
        }
    }

    // 第一种方法
    // 收集整棵树的高度h,和节点数n
    // 只有满二叉树满足 : 2 ^ h - 1 == n
    public static boolean isFull1(Node head) {if (head == null) {return true;
        }
        Info1 all = process1(head);
        return (1<< all.height) - 1 == all.nodes;
    }

    public static class Info1 {public int height;
        public int nodes;

        public Info1(int h, int n) {height = h;
            nodes = n;
        }
    }

    public static Info1 process1(Node head) {if (head == null) {return new Info1(0, 0);
        }
        Info1 leftInfo = process1(head.left);
        Info1 rightInfo = process1(head.right);
        int height = Math.max(leftInfo.height, rightInfo.height) + 1;
        int nodes = leftInfo.nodes + rightInfo.nodes + 1;
        return new Info1(height, nodes);
    }

    // 第二种方法
    // 收集子树是否是满二叉树
    // 收集子树的高度
    // 左树满 && 右树满 && 左右树高度一样 ->整棵树是满的
    public static boolean isFull2(Node head) {if (head == null) {return true;
        }
        return process2(head).isFull;
    }

    public static class Info2 {public boolean isFull;
        public int height;

        public Info2(boolean f, int h) {isFull = f;
            height = h;
        }
    }

    public static Info2 process2(Node h) {if (h == null) {return new Info2(true, 0);
        }
        Info2 leftInfo = process2(h.left);
        Info2 rightInfo = process2(h.right);
        boolean isFull = leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height;
        int height = Math.max(leftInfo.height, rightInfo.height) + 1;
        return new Info2(isFull, height);
    }

    // for test
    public static Node generateRandomBST(int maxLevel, int maxValue) {return generate(1, maxLevel, maxValue);
    }

    // for test
    public static Node generate(int level, int maxLevel, int maxValue) {if (level >maxLevel || Math.random()< 0.5) {return null;
        }
        Node head = new Node((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    public static void main(String[] args) {int maxLevel = 5;
        int maxValue = 100;
        int testTimes = 1000000;
        System.out.println("测试开始");
        for (int i = 0; i< testTimes; i++) {Node head = generateRandomBST(maxLevel, maxValue);
            if (isFull1(head) != isFull2(head)) {System.out.println("出错了!");
            }
        }
        System.out.println("测试结束");
    }
}
更多

算法和数据结构笔记

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站名称:判断二叉树是否为满二叉树-创新互联
文章URL:http://cdxtjz.cn/article/cssjos.html

其他资讯