PAT甲级 1135. Is It A Red-Black Tree (30)

本文介绍了一种通过先序遍历来判断给定二叉搜索树是否为合法红黑树的方法。红黑树是一种自平衡二叉查找树,具备特定的颜色属性及平衡条件。文章详细解释了红黑树的五个特性,并提供了实现代码,通过构造二叉搜索树并检查其是否符合红黑树的定义。

https://www.patest.cn/contests/pat-a-practise/1135

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.

Figure 1
Figure 2
Figure 3

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
其实这道题我不是很懂,我也不知道怎么通过先序遍历构造平衡二叉树,后来磨了很久做了出来,不过可能方法比较奇怪。
先根据题目所给的先序遍历构造二叉搜索树(其实这样的话就和输入顺序没有关系了),然后逐条检查红黑树的特征,前三条很容易检查,然后traversal1()检查第四条,traversal2()检查第五条。
preordert()输出我构造的二叉树的先序遍历,用来检查是否和输入的先序遍历一样。不过,即使加了这一个功能,得分也没有变化。
第三个测试点不知为什么过不了,丢了一分

#include
#include
using namespace std;

enum color {
	red,
	black
};
struct Node {
	int data;
	color colour;
	Node * left;
	Node * right;
	Node() {
		data = 0;
		colour = black;
		left = NULL;
		right = NULL;
	}
};

void addchild(int num, Node * tree)
{
	int abs = num<0 ? -num : num;
	if (tree->data>abs)
		if (tree->left == NULL)
		{
			Node * newnode = new Node;
			newnode->data = abs;
			newnode->colour = num<0 ? red : black;
			tree->left = newnode;
		}
		else
			addchild(num, tree->left);
	else
	{
		if (tree->right == NULL)
		{
			Node * newnode = new Node;
			newnode->data = abs;
			newnode->colour = num<0 ? red : black;
			tree->right = newnode;
		}
		else
			addchild(num, tree->right);
	}
}

bool traversal1(Node * tree)
{
	bool islegal = true;
	if (tree->colour == red)
		if ((tree->left != NULL&&tree->left->colour == red) || (tree->right != NULL&&tree->right->colour == red))
			return false;
	if (tree->left != NULL)
		islegal &= traversal1(tree->left);
	if (tree->right != NULL)
		islegal &= traversal1(tree->right);
	return islegal;
}

int blacknum = -1;
bool traversal2(Node * tree, int numofblack)
{
	if (tree->colour == black)numofblack++;
	if (tree->left == NULL&&tree->right == NULL)
	{
		if (blacknum == -1)
			blacknum = numofblack;
		else
			if (blacknum != numofblack)
				return false;
	}
	bool islegal = true;
	if (tree->left != NULL)
		islegal &= traversal2(tree->left, numofblack);
	if (tree->right != NULL)
		islegal &= traversal2(tree->right, numofblack);
	return islegal;
}

vector pre;
void preordert(Node * tree)
{
	pre.push_back(tree->colour == black ? tree->data : -tree->data);
	if (tree->left != NULL)
		preordert(tree->left);
	if (tree->right != NULL)
		preordert(tree->right);
}
int main()
{
	int K;
	cin >> K;
	for (int i = 0; i> N;
		int input;
		cin >> input;
		inputs[0] = input;
		if (input <= 0)
			islegal = false;
		treeroot->data = input<0 ? -input : input;
		treeroot->colour = input<0 ? red : black;
		for (int j = 0; j> input;
			inputs[j + 1] = input;
			if (input == 0)
				islegal = false;
			addchild(input, treeroot);
		}
		pre.clear();
		preordert(treeroot);
		for (int j = 0; j


<think>我们正在解决用户执行`git credential-manager delete https://github.com`时出现`git: 'credential-manager' is not a git command`错误的问题。 根据引用[1]中的信息,这个错误通常是由于Git找不到`credential-manager`命令引起的。引用[1]提供了两种解决方法:修改配置文件中的`credential.helper`为`manager-core`或`manager`。 但注意,用户尝试执行的命令是`git credential-manager delete`,这需要确保Git能够识别`credential-manager`作为子命令。而实际上,在Git Credential Manager (GCM)的安装中,这个命令通常是通过一个可执行文件`git-credential-manager`(或`git-credential-manager-core`)提供的,并且应该位于Git的执行路径下。 因此,我们的解决步骤应该是: 1. 确认GCM是否安装,并且可执行文件在Git的exec-path目录中。 2. 检查配置中`credential.helper`的设置,确保指向正确的助手(manager或manager-core)。 3. 如果上述步骤无效,则可能需要重新安装GCM。 然而,用户现在遇到的是执行命令本身就不识别,所以我们首先需要让`git credential-manager`命令可用。 另外,根据引用[3],我们知道GitHub现在要求使用个人访问令牌(PAT)而不是密码,所以如果用户需要重新认证,要提醒用户使用PAT。 具体解决步骤: 步骤1:检查Git的exec-path目录是否存在GCM的可执行文件 - 运行`git --exec-path`查看Git的执行路径,然后去这个目录下查看是否存在`git-credential-manager`(或`git-credential-manager-core`)可执行文件。 - 如果不存在,则需要安装GCM。 步骤2:安装或更新GCM - 根据操作系统安装GCM(参考引用[1]中提供的链接或使用包管理器安装)。 步骤3:配置`credential.helper` - 设置全局配置:`git config --global credential.helper manager`(或者`manager-core`,具体取决于安装的版本) 步骤4:如果已经安装但命令不可用,检查PATH环境变量 - 在Linux/macOS上,可能需要将GCM的安装目录添加到PATH。例如,如果GCM安装在`/usr/local/share/gcm-core`,则添加: export PATH="$PATH:/usr/local/share/gcm-core" - 在Windows上,重新安装Git for Windows并确保勾选“Git Credential Manager”组件。 步骤5:使用正确的命令删除凭证 - 一旦GCM正确安装和配置,可以使用以下命令删除凭证: git credential-manager delete https://github.com - 或者,如果配置的是`manager-core`,可能命令是`git credential-manager-core delete https://github.com`(但注意,新版本已经统一为`git-credential-manager`,并且通过配置的helper别名调用,所以直接使用`git credential-manager`应该可以)。 但是,在GCM的官方文档中,更推荐使用`git credential-manager`命令(新版本)或者使用配置的helper机制(通过`git credential reject`来删除凭证)。因此,我们也可以教用户使用另一种方式删除凭证: 备选删除凭证方法(使用Git内置的凭证工具): ```bash echo url=https://github.com | git credential reject ``` 步骤6:如果问题仍然存在,尝试清除所有凭证缓存(如果GCM支持) ```bash git credential-manager delete --all ``` 注意:在Windows上,如果使用Git Bash,可能需要以管理员权限运行。 另外,根据引用[1]中提到的,有时全局配置和系统配置冲突,需要检查所有配置层级: - 运行`git config --list --show-origin`,查看所有配置的来源,确保没有重复配置`credential.helper`导致冲突。 因此,我们可以为用户总结如下步骤: ### 解决步骤: 1. **确认GCM安装**: - 打开终端(或Git Bash),运行: ```bash git --exec-path ``` 进入该目录(例如:`cd /usr/lib/git-core` 或 Windows上类似`C:\Program Files\Git\mingw64\libexec\git-core`),查看是否存在以下任一文件: - `git-credential-manager` - `git-credential-manager-core` - `git-credential-manager.exe` (Windows) 如果不存在,则需要安装GCM。 2. **安装GCM**: - **Windows**:重新安装[Git for Windows](https://git-scm.com/download/win),确保安装时勾选“Git Credential Manager”。 - **macOS**: ```bash brew install git-credential-manager ``` - **Linux**: 下载对应版本的.deb或.rpm包,或使用二进制安装(参考官方文档:https://github.com/GitCredentialManager/git-credential-manager) 例如,对于Ubuntu: ```bash wget https://github.com/GitCredentialManager/git-credential-manager/releases/download/v2.3.2/gcm-linux_amd64.2.3.2.deb sudo dpkg -i gcm-linux_amd64.2.3.2.deb git-credential-manager configure ``` 3. **配置环境变量(Linux/macOS)**: 如果安装后命令仍然找不到,可能需要将GCM的路径添加到PATH: ```bash echo 'export PATH="$PATH:/usr/local/share/gcm-core"' >> ~/.bashrc # 或 ~/.zshrc source ~/.bashrc ``` 4. **检查并设置`credential.helper`**: ```bash # 查看当前配置 git config --global --get credential.helper git config --system --get credential.helper # 可能需要管理员权限 # 设置为manager(推荐) git config --global credential.helper manager ``` 5. **尝试删除凭证**: - 方法1(推荐): ```bash git credential-manager delete https://github.com ``` - 方法2(使用Git内置凭证工具): ```bash echo url=https://github.com | git credential reject ``` - 方法3(如果上述都不行,清除所有凭证): ```bash git credential-manager delete --all ``` 6. **如果仍然报错**: - 检查是否有多个版本的Git或配置冲突: ```bash git config --list --show-origin ``` 检查所有`credential.helper`的配置项,确保没有冲突(例如,全局配置和系统配置不一致)。如果有冲突,可以编辑对应的配置文件(全局配置通常是`~/.gitconfig`,系统配置可能是`/etc/gitconfig`)删除冲突的配置。 ### 注意事项: - 如果用户在使用PAT(个人访问令牌)进行认证,在删除凭证后再次操作(如`git pull`)时,需要重新输入用户名和PAT(而不是密码)。 - 在Windows上,凭证存储在Windows凭据管理器中,也可以手动删除:打开“控制面板” -> “用户账户” -> “管理Windows凭据”,在“普通凭据”中删除Git相关的凭据。 ### 流程图解: ```mermaid graph TD A[开始] --> B[检查GCM可执行文件是否存在] B -->|存在| C[检查credential.helper配置] B -->|不存在| D[安装GCM] D --> C C --> E[设置credential.helper为manager] E --> F[尝试删除凭证] F --> G{成功?} G -->|是| H[结束] G -->|否| I[检查配置冲突] I --> J[清除冲突配置] J --> F ``` ### 相关问题: §§相关问题§§ 1. 如何检查Git Credential Manager是否正确安装? 2. 在Windows上如何手动删除Git保存的凭证? 3. 为什么需要设置`credential.helper`为manager? 4. 如何生成GitHub的个人访问令牌(PAT)? 5. 除了Git Credential Manager,还有哪些Git凭证存储方式? 请根据此框架生成回答。 </think> ### 解决 `git: 'credential-manager' is not a git command` 错误的方法 当执行 `git credential-manager delete https://github.com` 出现此错误时,表明系统未正确识别 Git Credential Manager (GCM) 命令。以下是逐步解决方案: --- #### 1. **验证 GCM 安装状态** ```bash # 检查 Git 执行路径 git --exec-path # 进入该目录查看是否存在 GCM 可执行文件 cd "$(git --exec-path)" ls -l git-credential-manager* # Linux/macOS dir git-credential-manager* # Windows ``` - ✅ 正常情况:应看到 `git-credential-manager` 或 `git-credential-manager-core` 文件 - ❌ 若不存在:需要[安装 GCM](#2-安装或更新-gcm) --- #### 2. **安装或更新 GCM** 根据操作系统选择安装方式: | 系统 | 安装命令 | |---------|-------------------------------------------------------------------------| | **macOS** | `brew install git-credential-manager` | | **Linux** | ```bash<br>wget https://github.com/GitCredentialManager/git-credential-manager/releases/download/v2.3.2/gcm-linux_amd64.2.3.2.deb<br>sudo dpkg -i gcm-linux_amd64.2.3.2.deb<br>git-credential-manager configure<br>``` | | **Windows** | 1. 重新安装 [Git for Windows](https://git-scm.com/download/win)<br>2. 安装时勾选 **Git Credential Manager** 组件 | --- #### 3. **配置全局凭证助手** ```bash # 设置全局配置 git config --global credential.helper manager # 验证配置 git config --global --get credential.helper # 应返回 "manager" ``` > ⚠️ 注意:旧版本可能使用 `manager-core`,若上述无效可尝试: > ```bash > git config --global credential.helper manager-core > ``` --- #### 4. **修复环境变量 (Linux/macOS)** 若命令仍不可用,添加 GCM 到 PATH: ```bash echo 'export PATH="$PATH:/usr/local/share/gcm-core"' >> ~/.bashrc # 或 ~/.zshrc source ~/.bashrc ``` --- #### 5. **替代凭证删除方法** 即使 GCM 命令不可用,仍可通过其他方式删除凭证: ```bash # 使用 Git 内置凭证工具 echo "protocol=https\nhost=github.com" | git credential reject # 或手动删除凭证文件 (Linux/macOS) rm ~/.git-credentials ``` --- #### 6. **检查配置冲突** ```bash # 查看所有配置源 git config --list --show-origin ``` - 若存在多个冲突的 `credential.helper` 设置: 1. 打开全局配置:`git config --global --edit` 2. 删除重复的 `[credential] helper=...` 条目 3. 保留唯一设置:`helper = manager` --- ### 配置验证流程图 ```mermaid graph TD A[错误出现] --> B{检查GCM文件是否存在?} B -->|不存在| C[安装GCM] B -->|存在| D[检查credential.helper配置] D --> E[设置为manager] E --> F[修复PATH环境变量] F --> G[使用替代命令删除凭证] G --> H[验证问题解决] ``` ### 关键点说明 1. **安装一致性**:Windows 系统必须通过 Git for Windows 安装器勾选 GCM 组件[^1] 2. **配置优先级**:局部仓库配置可能覆盖全局设置,使用 `--show-origin` 排查冲突 3. **认证更新**:删除凭证后操作 Git 会触发重新认证,需使用 [GitHub PAT](https://github.com/settings/tokens) 而非密码[^3]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值