【LeetCode】Same Tree

本文介绍了如何判断两棵二叉树是否等价的方法,包括递归、深度优先遍历和广度优先遍历三种思路,并提供了C++实现代码。

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


题目描述

Same Tree

 

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.


题目分析

判断两棵树是否相,就是把一棵对遍历一边,逐个判断是否相同
三种思路
1-递归
2-循环栈,深度优先遍历
3-循环队列,广度优先遍历

总结

对于对比的两棵树,某个结点要存在都应该存在,要不存在都应该不存在。

代码示例

#include 
  
   
#include 
   
    
#include 
    
     
using namespace std;
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 };
#if 0//递归法 	
//无论是深度还是广度,遍历一遍就可以了。 
class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        if(p == NULL || q == NULL)
		{
			//printf("return\n");
			if(p == q)	return true;
			else		return false;
		} 
		if(p->val == q->val)
		{
			//printf("%d\n",p->val);
			if(!isSameTree(p->left,q->left))	return false;
			if(!isSameTree(p->right,q->right))	return false;
			return true;
		}
		else 
		{
			//printf("%d--%d\n",p->val,q->val);
			return false;
		}
    }
};
#elif 1//广度优先遍历 
class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        if(p == NULL || q == NULL)
		{
			//printf("return\n");
			if(p == q)	return true;
			else		return false;
		} 
		queue
     
       pque, qque;
		pque.push(p),qque.push(q);
		TreeNode *tmpp, *tmpq; 
		while((!pque.empty()) && (!qque.empty()))
		{
			tmpp = pque.front(),tmpq = qque.front();	//能进循环的栈一定不是空,所以不用判断空 
			if(tmpp->val != tmpq->val)	return false;
				pque.pop(),qque.pop();
			
			if(tmpq->left != NULL && tmpp->left != NULL)		//左孩子要不都存在,要不都不存在 
				 pque.push(tmpp->left),qque.push(tmpq->left);
		 	else if((tmpq->left != NULL) || (tmpp->left != NULL))	
		 		return false;	
		 		
			if(tmpq->right != NULL && tmpp->right != NULL)		//右孩子要不都存在,要不都不存在 
				 pque.push(tmpp->right),qque.push(tmpq->right);
		 	else if((tmpq->right != NULL) || (tmpp->right != NULL))	
		 		return false;				 	
		} 
		if(pque.empty() && qque.empty())	return true;
		else								return false;
    }
};

#elif 1//深度优先 栈 
class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        if(p == NULL || q == NULL)
		{
			//printf("return\n");
			if(p == q)	return true;
			else		return false;
		} 
		stack
      
        pstk, qstk;
		pstk.push(p),qstk.push(q);
		TreeNode *tmpp, *tmpq; 
		while((!pstk.empty()) && (!qstk.empty()))
		{
			tmpp = pstk.top(),tmpq = qstk.top();	//能进循环的栈一定不是空,所以不用判断空 
			if(tmpp->val != tmpq->val)	return false;
				pstk.pop(),qstk.pop();
			
			if(tmpq->right != NULL && tmpp->right != NULL)		//右孩子要不都存在,要不都不存在 
				 pstk.push(tmpp->right),qstk.push(tmpq->right);
		 	else if((tmpq->right != NULL) || (tmpp->right != NULL))	
		 		return false;
		 		
			if(tmpq->left != NULL && tmpp->left != NULL)		//左孩子要不都存在,要不都不存在 
				 pstk.push(tmpp->left),qstk.push(tmpq->left);
		 	else if((tmpq->left != NULL) || (tmpp->left != NULL))	
		 		return false;		 	
		} 
		if(pstk.empty() && qstk.empty())	return true;
		else								return false;
    }
};

#endif 

/*功能测试*/
void test0()
{
/*
				1
		2				3
	4		5		6		7
*/ 
	TreeNode node1(1);	
	TreeNode node2(2);
	TreeNode node3(3);
	TreeNode node4(4);
	TreeNode node5(5);
	TreeNode node6(6);
	TreeNode node7(7);
	
	node1.left = &node2;
	node1.right = &node3;
	
	node2.left = &node4;
	node2.right = &node5;
	
	node3.left = &node6;
	node3.right = &node7;
	
	
	Solution so;
	bool ret = so.isSameTree(&node1,&node1);
	
	if(ret)
		printf("---------------------passed\n");
	else
		printf("---------------------failed\n");	
}
/*功能测试*/
void test1()
{
	/*
				1
		2				3
	4		5		6		7
*/ 
	TreeNode node1(1);
	TreeNode node2(2);
	TreeNode node3(3);
	TreeNode node4(4);
	TreeNode node5(5);
	TreeNode node6(6);
	TreeNode node7(7);
	node1.left = &node2;
	node1.right = &node3;
	
	node2.left = &node4;
	node2.right = &node5;
	
	node3.left = &node6;
	node3.right = &node7;

	/*
				1
		2				3
	4		5		6
*/ 
	TreeNode nodeq1(1);
	TreeNode nodeq2(2);
	TreeNode nodeq3(3);
	TreeNode nodeq4(4);
	TreeNode nodeq5(5);
	TreeNode nodeq6(6);
	nodeq1.left = &nodeq2;
	nodeq1.right = &nodeq3;
	
	nodeq2.left = &nodeq4;
	nodeq2.right = &nodeq5;
	
	nodeq3.left = &nodeq6;
	
	Solution so;
	bool ret = so.isSameTree(&node1,&nodeq1);
	
	if(!ret)
		printf("---------------------passed\n");
	else
		printf("---------------------failed\n");	
}
/*功能测试*/
void test2()
{
	/*
				1
		2				3
	4		5		6
*/ 
	TreeNode node1(1);
	TreeNode node2(2);
	TreeNode node3(3);
	TreeNode node4(4);
	TreeNode node5(5);
	TreeNode node6(6);
	node1.left = &node2;
	node1.right = &node3;
	
	node2.left = &node4;
	node2.right = &node5;
	
	node3.left = &node6;

	/*
				1
		2				3
	4		5		7
*/ 
	TreeNode nodeq1(1);
	TreeNode nodeq2(2);
	TreeNode nodeq3(3);
	TreeNode nodeq4(4);
	TreeNode nodeq5(5);
	TreeNode nodeq6(7);
	nodeq1.left = &nodeq2;
	nodeq1.right = &nodeq3;
	
	nodeq2.left = &nodeq4;
	nodeq2.right = &nodeq5;
	
	nodeq3.left = &nodeq6;
	
	Solution so;
	bool ret = so.isSameTree(&node1,&nodeq1);
	
	if(!ret)
		printf("---------------------passed\n");
	else
		printf("---------------------failed\n");	
}

int main()
{
	test0();
	test1();
	test2();
	return 0;
}


      
     
    
   
  


推荐学习C++的资料

C++标准函数库
在线C++API查询

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值