有苦有乐的算法 --- 判断一颗二叉树是否是完全二叉树、是否是平衡二叉树、是否是搜索二叉树

25 篇文章 0 订阅

是否是完全二叉树

完全二叉树:二叉树的每一层要么是满的,要么从左到右处在变满的路上。

public static boolean isCBT(Node head) {
	if (head == null) {
		return true;
	}
	return process(head).isCBT;
}

public static class Info {
	public boolean isFull;
	public boolean isCBT;
	public int height;
	public Info(boolean full, boolean cbt, int h) {
		isFull = full;
		isCBT = cbt;
		height = h;
	}
}

public static Info process(Node X) {
	if (X == null) {
		return new Info(true, true, 0);
	}
	Info leftInfo = process(X.left);
	Info rightInfo = process(X.right);
	int height = Math.max(leftInfo.height, rightInfo.height) + 1;
	boolean isFull = leftInfo.isFull 
			&& rightInfo.isFull 
			&& leftInfo.height == rightInfo.height;
	boolean isCBT = false;
	if (isFull) {
		isCBT = true;
	} else { // 以x为头整棵树,不满
		if (leftInfo.isCBT && rightInfo.isCBT) {
			if (leftInfo.isCBT 
					&& rightInfo.isFull 
					&& leftInfo.height == rightInfo.height + 1) {
				isCBT = true;
			}
			if (leftInfo.isFull 
					&& 
					rightInfo.isFull 
					&& leftInfo.height == rightInfo.height + 1) {
				isCBT = true;
			}
			if (leftInfo.isFull 
					&& rightInfo.isCBT && leftInfo.height == rightInfo.height) {
				isCBT = true;
			}
		}
	}
	return new Info(isFull, isCBT, height);
}

是否是平衡二叉树

平衡二叉树:二叉树的任何一个节点,此节点的左子树高度的右子树的高度差<=1.

public static boolean isBalanced(Node head) {
	return process(head).isBalanced;
}

public static class Info{
	public boolean isBalanced;
	public int height;
	
	public Info(boolean i, int h) {
		isBalanced = i;
		height = h;
	}
}

public static Info process(Node x) {
	if(x == null) {
		return new Info(true, 0);
	}
	Info leftInfo = process(x.left);
	Info rightInfo = process(x.right);
	int height = Math.max(leftInfo.height, rightInfo.height)  + 1;
	boolean isBalanced = true;
	if(!leftInfo.isBalanced) {
		isBalanced = false;
	}
	if(!rightInfo.isBalanced) {
		isBalanced = false;
	}
	if(Math.abs(leftInfo.height - rightInfo.height) > 1) {
		isBalanced = false;
	}
	return new Info(isBalanced, height);
}

是否是搜索二叉树

搜索二叉树:二叉树的任何一个节点,此节点的左子节点的值小于此节点的值,左子节点的值大于此节点的值

public static boolean isBST(Node head) {
	if (head == null) {
		return true;
	}
	return process(head).isBST;
}

public static class Info {
	public boolean isBST;
	public int max;
	public int min;
	public Info(boolean i, int ma, int mi) {
		isBST = i;
		max = ma;
		min = mi;
	}
}

public static Info process(Node x) {
	if (x == null) {
		return null;
	}
	Info leftInfo = process(x.left);
	Info rightInfo = process(x.right);
	int max = x.value;
	if (leftInfo != null) {
		max = Math.max(max, leftInfo.max);
	}
	if (rightInfo != null) {
		max = Math.max(max, rightInfo.max);
	}
	int min = x.value;
	if (leftInfo != null) {
		min = Math.min(min, leftInfo.min);
	}
	if (rightInfo != null) {
		min = Math.min(min, rightInfo.min);
	}
	boolean isBST = true;
	if (leftInfo != null && !leftInfo.isBST) {
		isBST = false;
	}
	if (rightInfo != null && !rightInfo.isBST) {
		isBST = false;
	}
	if (leftInfo != null && leftInfo.max >= x.value) {
		isBST = false;
	}
	if (rightInfo != null && rightInfo.min <= x.value) {
		isBST = false;
	}
	return new Info(isBST, max, min);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值