- 平衡二叉树:任何一个节点的左右子树高度只差 <=1
- 二叉搜索树(BST,Binary Search Tree):对于一棵树上任何一个节点的子树,左子树 < 节点 < 右子树 。通常不出现重复节点,如果有重复节点,可以把它们的值压缩在一个节点的内部。
- 完全二叉树(CBT,Complete Binary Tree):①某个节点有右孩子没有左孩子,则不是完全二叉树;②满足①的条件下,某个节点没有右孩子,有左孩子,或者没有左右孩子时,后面遇到的所有节点必须是叶节点。
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Code_07_IsBSTAndCBT {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static class ReturnData {
public boolean isB;
public int h;
public ReturnData(boolean isB, int h) {
this.isB = isB;
this.h = h;
}
}
public static ReturnData process(Node head) {
if (head == null) {
return new ReturnData(true, 0);
}
ReturnData leftData = process(head.left);
if (!leftData.isB) {
return new ReturnData(false, 0);
}
ReturnData rightData = process(head.right);
if (!rightData.isB) {
return new ReturnData(false, 0);
}
if (Math.abs(leftData.h - rightData.h) > 1) {
return new ReturnData(false, 0);
}
return new ReturnData(true, Math.max(leftData.h, rightData.h) + 1);
}
public static Boolean isB(Node head) {
return process(head).isB;
}
int pre = Integer.MIN_VALUE;
public static Boolean isBST1(Node head) {
boolean res = true;
if (head == null) {
return res;
}
isBST1(head.left);
if (head.value > pre) {
pre = head.value;
} else {
res = false;
}
isBST1(head.right);
return res;
}
public static Boolean isBST2(Node head) {
int pre = Integer.MIN_VALUE;
boolean res = true;
if (head != null) {
Stack<Node> stack = new Stack<>();
while (!stack.isEmpty() || head != null) {
if (head != null) {
stack.push(head);
head = head.left;
} else {
head = stack.pop();
if (head.value > pre) {
pre = head.value;
} else {
res = false;
}
head = head.right;
}
}
}
return res;
}
public static boolean isCBT(Node head) {
if (head == null) {
return true;
}
Queue<Node> queue = new LinkedList<Node>();
boolean leaf = false;
Node l = null;
Node r = null;
queue.offer(head);
while (!queue.isEmpty()) {
head = queue.poll();
l = head.left;
r = head.right;
if ((leaf && (l != null || r != null)) || (l == null && r != null)) {
return false;
}
if (l != null) {
queue.offer(l);
}
if (r != null) {
queue.offer(r);
}
if (l == null || r == null) {
leaf = true;
}
}
return true;
}
public static void printTree(Node head) {
System.out.println("Binary Tree:");
printInOrder(head, 0, "H", 17);
System.out.println();
}
public static void printInOrder(Node head, int height, String to, int len) {
if (head == null) {
return;
}
printInOrder(head.right, height + 1, "v", len);
String val = to + head.value + to;
int lenM = val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
System.out.println(getSpace(height * len) + val);
printInOrder(head.left, height + 1, "^", len);
}
public static String getSpace(int num) {
String space = " ";
StringBuffer buf = new StringBuffer("");
for (int i = 0; i < num; i++) {
buf.append(space);
}
return buf.toString();
}
public static void main(String[] args) {
Node head = new Node(5);
head.left = new Node(3);
head.right = new Node(8);
head.left.left = new Node(2);
head.left.right = new Node(4);
head.right.left = new Node(6);
head.right.right = new Node(10);
head.left.left.left = new Node(1);
head.right.left.right = new Node(7);
head.right.right.left = new Node(9);
printTree(head);
System.out.println(isB(head));
System.out.println(isBST1(head));
System.out.println(isBST2(head));
System.out.println(isCBT(head));
}
}