PAT-1135 Is It A Red-Black Tree(二叉查找树的创建和遍历)
There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties:
(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.
For example, the tree in Figure 1 is a red-black tree, while the ones in Figure 2 and 3 are not.
For each given binary search tree, you are supposed to tell if it is a legal red-black tree.
Input Specification:
Each input file contains several test cases. The first line gives a positive integer K (<=30) which is the total number of cases. For each case, the first line gives a positive integer N (<=30), the
 total number of nodes in the binary tree. The second line gives the preorder traversal sequence of the tree. While all the keys in a tree are positive integers, we use negative signs to represent red nodes. All the numbers in a line are separated by a space.
 The sample input cases correspond to the trees shown in Figure 1, 2 and 3.
Output Specification:
For each test case, print in a line "Yes" if the given tree is a red-black tree, or "No" if not.
Sample Input:
3
9
7 -2 1 5 -4 -11 8 14 -15
9
11 -2 1 -7 5 -4 8 14 -15
8
10 -7 5 -6 8 15 -11 17
Sample Output:
Yes
No
No
题目大意:这道题要求判断给出的树是不是红黑树,用节点值加负号的方式表示红色节点。其中红黑树需要满足三个主要条件:(1)根节点是黑色 (2)红色节点的子节点均为黑色节点(包括NULL) (3) 所有从根节点到叶子节点的路径上经过的黑色节点数目相同。
主要思路:题目考查的其实与红黑树关系不大,主要是二叉查找树的建立和遍历。首先根据给出的一系列节点创建二叉树,这里用到的是最基本的性质,左子树节点的值小于根节点的值且小于右子树节点的值,利用递归的方法将给出的节点插入树中。创建好之后,再利用递归的先序遍历对以上要求一一判断即可完成。
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef struct node {
    int val;
    struct node * left;
    struct node * right;
} Node;
Node * root;								//根节点
int num_black;								//黑色节点数量
bool is_red_black;							//是否是红黑树
//动态分配新的节点
Node * new_node(int val) {
    Node * p = (Node *) malloc(sizeof(Node));
    p->val = val;
    p->left = NULL;
    p->right = NULL;
    return p;
}
//添加节点
Node * add_node(Node * node, int val) {
    if (node == NULL)   return new_node(val);
    if (abs(val) < abs(node->val))
        node->left = add_node(node->left, val);
    else
        node->right = add_node(node->right, val);
    return node;
}
void add(int val) {
    root = add_node(root, val);
}
//先序遍历
void travel(Node * node, int num) {
    if (!is_red_black)      return;			//如果已经发现不是红黑树,停止后面的递归
    if (node == NULL) {						//遍历到NULL以后,判断路径中的黑色节点数是否与之前一致
        if (num_black == 0)
            num_black = num;
        else if (num_black != num)
            is_red_black = false;
        return;
    }
    if (node->val < 0) {					//红色节点:子节点必须为黑色节点(或NULL)
        if ((node->left != NULL && node->left->val < 0) || (node->right != NULL && node->right->val < 0))
            is_red_black = false;
    }
    else									//黑色节点:计数+1
        num++;
    travel(node->left, num);
    travel(node->right, num);
}
int main(void) {
    int k, n, i, j;
    cin >> k;
    for (i = 0; i < k; i++) {
        root = NULL;
        num_black = 0;
        is_red_black = true;
        cin >> n;
		//创建二叉树
        for (j = 0; j < n; j++) {
            int v;
            cin >> v;
            add(v);
        }
        if (root->val < 0)					//红黑树根节点为黑色
            is_red_black = false;
        travel(root, 0);
        if (is_red_black)
            printf("Yes\n");
        else
            printf("No\n");
    }
	return 0;
}PAT-1135 Is It A Red-Black Tree(二叉查找树的创建和遍历)的更多相关文章
- PAT A1135 Is It A Red Black Tree
		判断一棵树是否是红黑树,按题给条件建树,dfs判断即可~ #include<bits/stdc++.h> using namespace std; ; struct node { int ... 
- PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca
		给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ... 
- PAT 1043 Is It a Binary Search Tree (25分) 由前序遍历得到二叉搜索树的后序遍历
		题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ... 
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
		题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ... 
- [转载] 红黑树(Red Black Tree)- 对于 JDK TreeMap的实现
		转载自http://blog.csdn.net/yangjun2/article/details/6542321 介绍另一种平衡二叉树:红黑树(Red Black Tree),红黑树由Rudolf B ... 
- Red–black tree ---reference wiki
		source address:http://en.wikipedia.org/wiki/Red%E2%80%93black_tree A red–black tree is a type of sel ... 
- Red Black Tree 红黑树   AVL trees  2-3 trees  2-3-4 trees  B-trees  Red-black trees  Balanced search tree  平衡搜索树
		小结: 1.红黑树:典型的用途是实现关联数组 2.旋转 当我们在对红黑树进行插入和删除等操作时,对树做了修改,那么可能会违背红黑树的性质.为了保持红黑树的性质,我们可以通过对树进行旋转,即修改树中某些 ... 
- PAT甲级:1066 Root of AVL Tree (25分)
		PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ... 
- PAT甲级:1064 Complete Binary Search Tree (30分)
		PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ... 
随机推荐
- 【杂谈】从实现角度看ChannelFuture
			JDK中的Future特性 在介绍Netty的ChannelFuture之前,我们先来看看JDK中的Future是如何实现的.总的来说就是任务提交的时候会使用装饰器模式,将任务包装成一个FutureT ... 
- CTR学习笔记&代码实现4-深度ctr模型 NFM/AFM
			这一节我们总结FM另外两个远亲NFM,AFM.NFM和AFM都是针对Wide&Deep 中Deep部分的改造.上一章PNN用到了向量内积外积来提取特征交互信息,总共向量乘积就这几种,这不NFM ... 
- Django入门4: ORM 数据库操作
			大纲 一.DjangoORM 创建基本类型及生成数据库表结构 1.简介 2.创建数据库 表结构 二.Django ORM基本增删改查 1.表数据增删改查 2.表结构修改 三.Django ORM 字段 ... 
- mac OS vi/vim 使用教程
			vi/vim 的使用 基本上 vi/vim 共分为三种模式 分别是 命令模式(Command mode) 输入模式(Insert mode) 底线命令模式(Last line mode) 命令模式: ... 
- VScode像Codeblocks一样,不启动调试和Debug直接运行
			要是配置C++ 编译环境,这边走 用了VScode童鞋,都知道,写C++是不保留窗口的,除非打上断点或者: system("pause"); 这里给大家分享一种不需要,F5或者Ct ... 
- Python安装第三方模块出错 No module named setuptools
			在安装 zabbix-alerta 第三方模块时候报错 python setup.py install 此时需要安装 setuptools 模块, 这里用自动化脚本安装 wget https://bo ... 
- django最全面的知识点,直接开发完整手机购物商城练手,
			带手机验证码登陆, 带全套购物车系统 带数据库 前后端分离开发 带定位用户功能 数据库代码为本地制作好了 带支付宝支付系统 带django开发服务器接口教程 地址: https://www.duans ... 
- 跟哥一起学python(2)- 运行第一个python程序&环境搭建
			本节的任务,是完成我们的第一个python程序,并搭建好学习python的环境. 建议通过视频来学习本节内容: 查看本节视频 再次看看上一节提到的那张图,看看作为高级编程语言,我们如何编程. 首先, ... 
- 从一条数据说起——InnoDB存储数据结构
			本篇博客参考掘金小册--MySQL 是怎样运行的:从根儿上理解 MySQL 先给大家讲一个故事,我刚参加工作,在一个小作坊里面当[码畜](尽管现在也是),有一天老板从我背后走过,说了一句举世震惊的话: ... 
- Spring Boot学习 之 Spring Boot Actuator(一)
			Spring Boot版本:2.1.4.RELEASE 启用: spring-boot-actuator模块提供了一系列的用于监控的端点.最简单的开启这个功能的方法就是,在pom文件中添加如下的依赖. ... 
