1.oj题
(1)相等二叉树
思路:首先判断这两棵树是否为空。再考虑其中一个是否为空,接着再比较值是否相等,最后,如果值相等再递归比较子树
1.判断两棵树是否相等。链接
bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if(p==NULL&&q==NULL)
{
return true;
}
if(p==NULL||q==NULL)//走到这p和q一定有一个为空,不然上面就return true了
{
return false;
}
if(p->val!=q->val)//如果值相等比不出结果(因为还要比较子树),所以直接判断不相等的情况
{
return false;
}
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);//当左树相等才判断右树,一个不相等就return false
}
时间复杂度为O(N)
(2) 对称二叉树
给定一个二叉树,检查它是否是镜像对称的。链接
思路:首先判断根是否相等,再递归比较根的左子树和右子树(比较的过程跟上一题类似),在这里可以写一个子函数,如果左子树和右子树的根相等,在拿其中一个的左树和另一个的右树比,右树和另一个的左树比。
bool _isSymmetric(struct TreeNode* root1,struct TreeNode* root2)
{
if(root1==NULL&&root2==NULL)
{
return true;
}
if(root1==NULL||root2==NULL)
{
return false;
}
if(root1->val!=root2->val)
{
return false;
}
//走到这一步,说明两个节点都不为空,且他们的值是相等的,接下来就要比较他们的子树
return _isSymmetric(root1->left,root2->right)&&_isSymmetric(root1->right,root2->left);
//先比较root1的左子树和root2的右子树是否相等,再比较root1的右子树和root2的左子树是否相等
}
bool isSymmetric(struct TreeNode* root){
if(root==NULL)//判断树是否为空
{
return true;
}
//再比较左右树
return _isSymmetric(root->left,root->right);
}
(3)另一棵树的子树
给你两棵二叉树 root 和 subRoot 。检验 root 中是否包含和 subRoot 具有相同结构和节点值的子树链接
思路:用root的每个子树(每个节点做根就是子树)都跟sub比较,所以也用到第一题的方法
bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if(p==NULL&&q==NULL)
{
return true;
}
if(p==NULL||q==NULL)//走到这p和q一定有一个为空,不然上面就return true了
{
return false;
}
if(p->val!=q->val)//如果值相等比不出结果(因为还要比较子树),所以直接判断不相等的情况
{
return false;
}
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);//当左树相等才判断右树,一个不相等就return false
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){
if(root==NULL)//判断root是否为空
{
return false;
}
if(isSameTree(root,subRoot))//root作为根的树是否和sub相等
{
return true;
}
return isSubtree(root->left,subRoot)||isSubtree(root->right,subRoot);//左子树和右子树一个和sub树相等就返回true
}
时间复杂度
最好的情况:一开始就是相等的,或者所有节点所在的子树(比较子树的根)都跟sub不相等
复杂度为O(N)。
最坏的情况:每个子树都要跟sub比较,所有都相等除了最后一个节点不相等
时间复杂度为O(n^2)
2.层序遍历
思路:
需要导入队列项目,前置声明很重要
struct BinaryTreeNode;
typedef struct BinaryTreeNode* QDataType;
再实现
void BinaryTreeLevelOrder(BTNode* root)//层序遍历
{
if (root == NULL)
{
return;
}
Queue q;
QueueInit(&q);
QueuePush(&q, root);
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);//删除头节点带入新的子节点
printf("%c ", front->data);
if (front->left)
{
QueuePush(&q, front->left);
}
if (front->right)
{
QueuePush(&q, front->right);
}
}
printf("\n");
QueueDestroy(&q);
}
在上面的基础上判断一棵树是不是完全二叉树
bool BinaryTreeComplete(BTNode* root)
{
Queue q;
QueueInit(&q);
QueuePush(&q, root);
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front == NULL)//遇到空
{
break;
}
else
{
QueuePush(&q, front->left);//即使左右节点为空也入进队列
QueuePush(&q, front->right);
}
}
//遇到空以后,检查队列中剩余节点
//1.剩下全是空,则是完全二叉树
//2.剩下存在非空,则不是完全二叉树
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front)
{
//每次返回前要先释放队列以免内存泄漏
QueueDestroy(&q);
return false;
}
}
QueueDestroy(&q);
return true;
}
二叉树遍历
#include<stdio.h>
#include<stdlib.h>
struct TreeNode
{
struct TreeNode* left;
struct TreeNode* right;
char val;
};
struct TreeNode* CreatTree(char* str,int* pi)//先序遍历创建
{
if(str[*pi]=='#')
{
(*pi)++;
return NULL;
}
struct TreeNode* root=(struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val=str[*pi];
(*pi)++;
root->left=CreatTree(str, pi);
root->right=CreatTree(str, pi);
return root;
}
void Inorder(struct TreeNode* root)//中序遍历打印
{
if(root==NULL)
{
return;
}
Inorder(root->left);
printf("%c ",root->val);
Inorder(root->right);
}
int main()
{
char str[100];
scanf("%s",str);
int i=0;
struct TreeNode* root=CreatTree(str,&i);
Inorder(root);
return 0;
}