PAT甲级1135 Is It A Red-Black Tree(JAVA版)

31 篇文章 0 订阅

本题考查

红黑树、树的深搜、树的广搜

思路

红黑树简介:https://zh.wikipedia.org/wiki/%E7%BA%A2%E9%BB%91%E6%A0%91

因为红黑树也是二叉搜索树,所以可以根据先序遍历顺序与元素相对大小构建树,然后使用BFS对每个元素进行判断,是否符合红黑树条件。

题目给了红黑树的五个条件:

  1. Every node is either red or black.
  2. The root is black.
  3. Every leaf (NULL) is black.
  4. If a node is red, then both its children are black.
  5. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.

第1、3不用判断,2,4在BFS过程中即可判断,5需要在BFS中对每个结点进行DFS,判断从该结点开始每条到null结点的路径的黑结点个数。

AC代码

import java.util.LinkedList;
import java.util.Scanner;
public class Main {
	static int blackNum = -1;
	static class Node {
		int color;// 0代表红,1代表黑
		int key;
		Node leftChild;
		Node rightChild;
		public Node(int color, int key) {
			this.color = color;
			this.key = key;
			this.leftChild = null;
			this.rightChild = null;
		}
	}
	static Node insert(Node root, int color, int key) {
		if (root == null) 			root = new Node(color, key);
		else if (key < root.key) 	root.leftChild = insert(root.leftChild, color, key);
		else if (key > root.key)	root.rightChild = insert(root.rightChild, color, key);
		return root;
	}
	static boolean dfs(Node root, int num) {
		boolean flag = true;
		if(root == null) {
			if(blackNum == -1)			blackNum = num;
			else if(blackNum != num) 	flag = false;
		}
		else {
			if(root.color == 1)              num++;
			if(!dfs(root.leftChild, num))    flag = false;
			if(!dfs(root.rightChild, num))   flag = false;
		}
		return flag;
	}
	static boolean bfs(Node root) {
		LinkedList<Node> list = new LinkedList<Node>();
		boolean flag = true;
		if(root.color == 0) flag = false;
		else {
			list.add(root);
			while(list.size() > 0) {
				Node n = list.remove();
				if(n.color == 0)
					if((n.leftChild != null && n.leftChild.color == 0)|| (n.rightChild != null && n.rightChild .color == 0)) {
						flag = false;
						break;
					}
				blackNum = -1;
				if(!dfs(n, 0)) { flag = false; break; }
				if(n.leftChild != null)  list.add(n.leftChild);
				if(n.rightChild != null) list.add(n.rightChild);
			}
		}
		return flag;
	}
	public static void main(String[] args) {
		Scanner scaner = new Scanner(System.in);
		int round = scaner.nextInt();
		Node root = null;
		for (int i = 0; i < round; i++) {
			int num = scaner.nextInt();
			root = null;
			int j;
			for (j = 0; j < num; j++) {
				int temp = scaner.nextInt();
				if (temp < 0) 	root = insert(root, 0, Math.abs(temp));
				else			root = insert(root, 1, Math.abs(temp));
			}
			if(bfs(root)) System.out.println("Yes");
			else          System.out.println("No");
		}
		scaner.close();
	}
}

提醒自己

勿忘全局变量blackNum的还原,第49行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值