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

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

## 方法1

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

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

``````Info1 process1(Node head)
``````

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

``````// 去左树上收集信息
// 去右树上收集信息
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;
}
}

``````

``````Info2 process2(Node head)
``````

base case是

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

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

``````    // 去左子树收集相关信息
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);
``````

``````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) {
return true;
}
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) {
return new Info1(0, 0);
}
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) {
return true;
}
}

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);
}

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++) {
System.out.println("出错了!");
}
}
System.out.println("测试结束");
}
}
``````

## 更多

原文作者：Grey Zeng
原文地址: https://www.cnblogs.com/greyzeng/p/16948391.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。