04-树4 是否同一棵二叉搜索树 (25分)

本文介绍了一种使用C++实现二叉搜索树的方法,包括树的构建、节点插入及验证输入序列是否符合已构建的二叉搜索树。通过递归方式实现了树的构建与遍历,并提供了用于验证序列一致性的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#include<iostream> 

using namespace std;  


struct TreeNode{
	int data;
	TreeNode * lchild;
	TreeNode * rchild;
	int flag;//0表示未访问过,1访问过 
};

TreeNode * NewTreeNode(int tmp){
	TreeNode * T=new TreeNode;
	T->data=tmp;  
	T->lchild=T->rchild=NULL;
	T->flag=0;	
	return T; 
}

TreeNode * Insert(TreeNode * T,int tmp){
	if(!T) T=NewTreeNode(tmp);
	else{                        //这个else必须要 
		if(tmp>T->data) T->rchild=Insert(T->rchild,tmp);
		else  T->lchild=Insert(T->lchild,tmp);	
	}

	return T;	
}


TreeNode * MakeTree(int N){
	int tmp; cin>>tmp;	
	TreeNode * T=NewTreeNode(tmp);//创建根节点 
	for(int i=1;i<N;i++){ //创建其它节点 
		cin>>tmp;
		T=Insert(T,tmp);
	} 
	return T;	
} 
  
  
bool Check(TreeNode * T,int tmp){
	
	if(T->flag){
		if(tmp>T->data) return Check(T->rchild,tmp); 
		else if(tmp<T->data)  return Check(T->lchild,tmp); 
		else return false;		
	}else{
		if(tmp==T->data){
			T->flag=1;
			return true;
		}else  return false;
	} 

	
}  
  
bool Judge(TreeNode * T,int N){
	int flag=0;//0表示当前还一致,1表示当前已经不一致 
	int tmp;cin>>tmp;
	if(T->data!=tmp) flag=1;
	else T->flag=1;//访问过
	//cout<<flag<<" "; 
	 
	for(int i=1;i<N;i++){
		cin>>tmp;
		//cout<<Check(T,tmp)<<" ";
		if( (!flag) && (!Check(T,tmp)) ) flag=1; //!flag表示当前还一致, !Check(T,tmp)表示接下来产生了不一致  !要打(),因为!优先级高于&& 
	}
	 
	if(flag) return false;
	else  return true;
}

void Reset(TreeNode * T){
	T->flag=0;
	if(T->lchild) Reset(T->lchild);
	if(T->rchild) Reset(T->rchild);		
}  


void FreeTree(TreeNode * T){
	if(T->lchild) FreeTree(T->lchild);
	if(T->rchild) FreeTree(T->rchild);	
	delete(T);
} 
  
  
int  main(){  
	//freopen("input.txt","r",stdin);
	int N,L;
	cin>>N;   
	while(N){  //if 
	cin>>L;  //这句要在建树之前,因为建树也要读取数据 
	TreeNode * T=MakeTree(N); 

	for(int i=0;i<L;i++){
		if(Judge(T,N)) cout<<"Yes"<<endl;
		else  cout<<"No"<<endl;
		Reset(T);//清除T中标记的flag,都初始化为0	
	}
	
	FreeTree(T); 
	cin>>N;//换一颗树 
	}
	
	
	return 0;  
}  







### 回答1: 题目描述: 给定两个整数序列,别表示两二叉搜索树的中序遍历序列,判断这两二叉搜索树是否同一。 解题思路: 二叉搜索树的中序遍历序列是有序的,因此我们可以将两个序列排序后比较是否相等。如果相等,则说明两二叉搜索树相同。 代码实现: ``` bool isSameBST(vector<int>& seq1, vector<int>& seq2) { if (seq1.size() != seq2.size()) { return false; } sort(seq1.begin(), seq1.end()); sort(seq2.begin(), seq2.end()); return seq1 == seq2; } ``` 时间复杂度:$O(nlogn)$ 空间复杂度:$O(n)$ 其中,$n$ 表示二叉搜索树中节点的个数。 ### 回答2: 题目描述: 给定两个长度相等的整型数组,判断它们是否对应同一二叉搜索树的后序遍历序列。 解题思路: 本题的难点在于如何判断两个数组是否对应同一二叉搜索树的后序遍历序列。 首先,我们知道二叉搜索树的后序遍历序列的最后一个元素为根节点,而且比根节点小的元素在左子中,比根节点大的元素在右子中。 因此,我们可以将数组前面的元素为左子和右子两个部,然后在子中递归检查,直到只剩下一个元素为止。 具体实现上,我们可以先找到根节点,然后将数组根据根节点为左右两个子数组,再递归检查左右子是否对应同一二叉搜索树的后序遍历序列。 如果左右子都满足条件,那么说明整个数组对应同一二叉搜索树的后序遍历序列,否则不是。 Code: ### 回答3: 题目描述: 给定两个长度为n的数组,按照给定的顺序插入到一空的二叉搜索树中,判断两个数组是否最终构建了同一二叉搜索树。 思路析: 本题是一道典型的二叉搜索树的应用题,题目要求我们判断两个经过插入操作后最终构建的是否相等。 首先回顾一下二叉搜索树的特点:对于一个二叉搜索树,它的左子中的所有节点的值都小于根节点的值,而它右子中的所有节点的值都大于根节点的值。 对于这个问题来说,我们可以在构建二叉搜索树的同时统计每个节点的左子大小来判断两是否相同。 具体来说,对于每个节点,我们可以统计它左子中的节点数目,然后每插入一个节点就更新一遍。当构建完毕后,每个节点的左子大小就都已经确定了。然后判断两个构建好的中所有节点的左子大小是否都一一对应即可。 代码实现: 为了方便起见,我们可以先实现一个二叉搜索树的插入操作,然后再利用这个代码来对两个输入排序数组进行构建。 //二叉搜索树结构体 struct TreeNode { int val; int left_size;//记录左子大小的变量 TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left_size(0), left(NULL), right(NULL) {} }; TreeNode* insert(TreeNode* root, int val){ if(!root) return new TreeNode(val); if(val < root->val){ root->left = insert(root->left, val); ++root->left_size;//更新左子大小 } else root->right = insert(root->right, val); return root; } 然后我们在主函数中对两个输入数组进行插入操作,之后再进行比较即可。 bool isSameTree(vector<int>& nums1, vector<int>& nums2) { if(nums1.size() != nums2.size()) return false; TreeNode* root1 = nullptr;//第一的根节点 TreeNode* root2 = nullptr;//第二的根节点 for(int i=0; i<nums1.size(); ++i){ root1 = insert(root1, nums1[i]); root2 = insert(root2, nums2[i]); } function<bool(TreeNode*, TreeNode*)> dfs = [&](TreeNode* node1, TreeNode* node2){ if(node1 == nullptr && node2 == nullptr) return true; else if(node1 == nullptr || node2 == nullptr) return false; else if(node1->val != node2->val || node1->left_size != node2->left_size) return false; else return dfs(node1->left, node2->left) && dfs(node1->right, node2->right); }; return dfs(root1, root2); } 时间复杂度:插入节点的时间复杂度是O(logn),所以整体时间复杂度是O(nlogn)。 空间复杂度:的深度最大为O(n),所以空间复杂度也是O(n)。 参考链接: https://leetcode-cn.com/problems/same-tree/description/ https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/description/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值