当前位置: 首页 > news >正文

【数据结构】二叉树练习

1.单值二叉树

965. 单值二叉树 - 力扣(LeetCode)

bool isUnivalTree(struct TreeNode* root) {if(root==NULL)return true;if(root->left&&root->left->val!=root->val)return false;if(root->right&&root->right->val!=root->val)return false;return isUnivalTree(root->left)&&isUnivalTree(root->right);
}

2.相同的树

100. 相同的树 - 力扣(LeetCode)

bool isSameTree(struct TreeNode* p, struct TreeNode* q) {if(p==NULL&&q==NULL)return true;if(p==NULL||q==NULL)return false;if(p->val!=q->val)return false;else{int lret=isSameTree(p->left,q->left);int rret=isSameTree(p->right,q->right);return lret&&rret;}
}

3.对称二叉树

101. 对称二叉树 - 力扣(LeetCode)

bool isSameTree(struct TreeNode* p,struct TreeNode* q)
{if(p==NULL&&q==NULL)return true;if(p==NULL||q==NULL)return false;if(p->val!=q->val)return false;return isSameTree(p->right,q->left)&&isSameTree(p->left,q->right);
} bool isSymmetric(struct TreeNode* root) {return isSameTree(root->left,root->right);
}

4.前序遍历(返回数组) 

144. 二叉树的前序遍历 - 力扣(LeetCode)

int TreeSize(struct TreeNode* root)
{return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
}void preorder(struct TreeNode* root,int*a,int*pi)
{if(root==NULL)return;a[(*pi)++]=root->val;preorder(root->left,a,pi);preorder(root->right,a,pi);}int* preorderTraversal(struct TreeNode* root, int* returnSize) {*returnSize=TreeSize(root);int*a=(int*)malloc(sizeof(int)*TreeSize(root));int i=0;preorder(root,a,&i);return a;
}

5.中序遍历(返回数组) 

94. 二叉树的中序遍历 - 力扣(LeetCode)

int TreeSize(struct TreeNode* root){return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;}void Inorder(struct TreeNode* root, int*a,int*pi){if(root==NULL)return;Inorder(root->left, a, pi);a[(*pi)++] = root->val;Inorder(root->right, a, pi);}int* inorderTraversal(struct TreeNode* root, int* returnSize) {*returnSize=TreeSize(root);int*a=(int*)malloc(sizeof(int)*TreeSize(root));int i=0;Inorder(root,a,&i);return a;
}

 6.后序遍历(返回数组)

145. 二叉树的后序遍历 - 力扣(LeetCode)

int TreeSize(struct TreeNode* root){return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;}void postorder(struct TreeNode* root, int*a,int*pi){if(root==NULL)return;postorder(root->left, a, pi);postorder(root->right, a, pi);a[(*pi)++] = root->val;}int* postorderTraversal(struct TreeNode* root, int* returnSize) {*returnSize=TreeSize(root);int*a=(int*)malloc(sizeof(int)*TreeSize(root));int i=0;postorder(root,a,&i);return a;
}

7.另一颗树的子树 

572. 另一棵树的子树 - 力扣(LeetCode)

 bool isSameTree(struct TreeNode* p,struct TreeNode* q)
{if(p==NULL&&q==NULL)return true;if(p==NULL||q==NULL)return false;if(p->val!=q->val)return false;return isSameTree(p->right,q->right)&&isSameTree(p->left,q->left);
} bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot) 
{if(root==NULL)return false;if(isSameTree(root,subRoot))return true;return isSubtree(root->right,subRoot)||isSubtree(root->left,subRoot);
}

8.二叉树遍历

#include <stdio.h>
#include<stdlib.h>struct TreeNode
{struct TreeNode*right;struct TreeNode*left;char val;
};struct TreeNode*CreatTree(char*a,int*pi)
{if(a[*pi]=='#'){(*pi)++;return NULL;}struct TreeNode* root=(struct TreeNode*)malloc(sizeof(struct TreeNode));root->val=a[*pi];(*pi)++;root->left=CreatTree(a,pi);root->right=CreatTree(a,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 a[100];scanf("%s",a);int i=0;struct TreeNode* root=CreatTree(a,&i);Inorder(root);return 0;
}

http://www.lryc.cn/news/613458.html

相关文章:

  • S7-1200 串行通信介绍
  • 一场 Dark Theme A/B 测试的复盘与提效实践
  • Linux上MySql CPU 占用异常
  • SpringBoot中的单例注入方式
  • windows有一个企业微信安装包,脚本执行并安装到d盘。
  • VSCode ssh一直在Setting up SSH Host xxx: Copying VS Code Server to host with scp等待
  • 开发避坑指南(20) :MyBatis操作Oracle插入NULL值异常“无效列类型1111“解决方案
  • DrissionPage实战案例:小红书旅游数据爬取
  • TDengine IDMP 文档介绍
  • 腾讯位置服务 —— 预估订单路线金额(使用Drools规则引擎处理)
  • 机器学习在量化中的应用:如何从逻辑回归到XGBoost实现高效预测?
  • [Oracle] DECODE()函数
  • DBeaver 25.1.0 转储数据库失败解决方案(适配最新版界面)
  • [Oracle] GREATEST()函数
  • 数据库入门:从零开始构建你的第一个数据库
  • 一个基于固定 IP地址查询天气的 C 语言程序,通过调用第三方天气 API:
  • Oracle 关闭 impdp任务
  • Oracle 12c + Pl/Sql windows系统下表空间创建、迁移,dmp备份导入,数据库字符集更改
  • 图论(1):图数据结构
  • 攻防世界WEB(新手模式)2-2-upload1
  • 【YOLO学习笔记】YOLOv8详解解读
  • 工业相机使用 YOLOv8深度学习模型 及 OpenCV 实现目标检测简单介绍
  • Moses工具的配置和小语种平行语料训练SMT完整实现
  • 商城小程序怎么做?如何开发母婴用品商城小程序?
  • 前端三大核心要素以及前后端通讯
  • mysql_mcp_server_pro源码部署及启动报错新手指南:让智能体长出手来直接获取到最底层的数据
  • Linux ISCSI服务配置
  • 聚集索引VS非聚集索引:核心差异详解
  • 将Excel数据导入SQL Server数据库,并更新源表数据
  • 安卓Handler和Looper的学习记录