当前位置:网站首页>二叉树oj题目
二叉树oj题目
2022-06-27 01:26:00 【编程SHARE】
二叉树
单值二叉树
解题思路
基本的解题思路就是比较根与左右两个节点的值是否相等。
在递归写法中,条件为归的条件,如果根为空,则返回真;根分别与左右两个孩子进行比较,不相同就返回假。最后再递左右两颗子树。
代码
bool isUnivalTree(struct TreeNode* root){
if(root==NULL)
return true;
if(root->right&&root->val!=root->right->val)
return false;
if(root->left&&root->val!=root->left->val)
return false;
return isUnivalTree(root->left)&&isUnivalTree(root->right);
}
二叉树的最大深度
题目描述
解题思路
树的高度就是路径最长的那一个,找树的高度就是比较左右子树的高度,子树高度最大的加1就是该树的高度。写递归的时候要注意函数的参数以及是否有返回值的情况。
先写出口,当节点为空的时候返回0,
递:比较左右子树的高度
归:最大高度
代码
int maxDepth(struct TreeNode* root){
if(root==NULL)
return 0;
int L=maxDepth(root->left)+1;
int R=maxDepth(root->right)+1;
return L>R?L:R;
}
相同的树
题目描述
解题思路
我们采取前序遍历的方法,先比较根节点,然后再比较左右子树。
只有根,左右子树都相同的时候才返回true。其余都返回false。
代码
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->left,q->left)&&isSameTree(p->right,q->right);
}
对称二叉树
题目描述
解题思路
除根节点外,左子树的左右节点与右子树的右左节点进行比较。
符合条件返回true,否则返回false。
代码
bool isSymmetryTree(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 isSymmetryTree(p->left,q->right)&&isSymmetryTree(p->right,q->left);
}
bool isSymmetric(struct TreeNode* root){
return isSymmetryTree(root->left,root->right);
}
二叉树的前序遍历
题目描述
解题思路
先求出有多少个节点,然后再前序遍历,前序遍历是先遍历根节点,然后遍历左右孩子节点。当节点不为空的时候把数据储存在数组中。
代码
int TreeSize(struct TreeNode* root)
{
if(root==NULL)
return 0;
return TreeSize(root->left)+TreeSize(root->right)+1;
}
void PreorderTree(struct TreeNode* root,int* a,int* n)
{
if(root==NULL)
return;
a[*n]=root->val;
++*n;
PreorderTree(root->left,a,n);
PreorderTree(root->right,a,n);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize=TreeSize(root);
int* arr=(int*)malloc((*returnSize)*sizeof(int));
int n=0;
PreorderTree(root,arr,&n);
return arr;
}
二叉树的中序遍历
题目描述
代码
int TreeSize(struct TreeNode* root)
{
if(root==NULL)
return 0;
return TreeSize(root->left)+TreeSize(root->right)+1;
}
void inorderTree(struct TreeNode* root,int* a,int* n)
{
if(root==NULL)
return;
inorderTree(root->left,a,n);
a[*n]=root->val;
++*n;
inorderTree(root->right,a,n);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize= TreeSize(root);
int* arr=(int*)malloc((*returnSize)*sizeof(int));
int n=0;
inorderTree(root,arr,&n);
return arr;
}
二叉树的后序遍历
题目描述
代码
int TreeSize(struct TreeNode* root)
{
if(root==NULL)
return 0;
return TreeSize(root->left)+TreeSize(root->right)+1;
}
void postorderTree(struct TreeNode* root,int* a,int* n)
{
if(root==NULL)
return;
postorderTree(root->left,a,n);
postorderTree(root->right,a,n);
a[*n]=root->val;
++*n;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize= TreeSize(root);
int* arr=(int*)malloc((*returnSize)*sizeof(int));
int n=0;
postorderTree(root,arr,&n);
return arr;
}
另一棵树的子树
题目描述
解题思路
遍历root树,当root节点的值与subroot根节点的值相同的时候,就开始比较它们是否相同,如果相同就返回true,否则要继续遍历左右子树。左右子树遍历的结果要||
。
代码
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->left,q->left)&&isSameTree(p->right,q->right);
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){
if(root==NULL)
return false;
if(root->val==subRoot->val)
if(isSameTree(root,subRoot))
return true;
return isSubtree(root->left,subRoot)||isSubtree(root->right,subRoot);
}
二叉树遍历
题目描述
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char BTDataType;
typedef struct BinaryTreeNode
{
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
BTDataType data;
}BTNode;
BTNode* BuyNode(char ch)
{
BTNode* newnode=(BTNode*)malloc(sizeof(BTNode));
newnode->data=ch;
newnode->left=newnode->right=NULL;
return newnode;
}
BTNode* CreatTree(char* arr,int n,int* i)
{
if(arr[*i]!='#'&&*i<n)
{
BTNode* root=BuyNode(arr[*i]);
++*i;
root->left=CreatTree(arr, n, i);
root->right=CreatTree(arr, n, i);
return root;
}
else
{
++*i;
return NULL;
}
}
void inorderTree(BTNode* root)
{
if(root==NULL)
return;
inorderTree(root->left);
printf("%c ",root->data);
inorderTree(root->right);
}
int main()
{
char arr[100];
scanf("%s",arr);
int len=strlen(arr);
int i=0;
BTNode* root=CreatTree(arr,len,&i);
inorderTree(root);
return 0;
}
边栏推荐
- JSON parsing, esp32 easy access to time, temperature and weather
- How to use ch423? Cheap domestic IO expansion chip
- Solve the problem that stc8g1k08 program cannot run and port configuration
- IIS deploy static web site and FTP service
- 30《MySQL 教程》MySQL 存储引擎概述
- 30 MySQL tutorial MySQL storage engine overview
- 通过Rust语言计算加速技术突破图片识别性能瓶颈
- TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
- Structure the fifth operation of the actual camp module
- 对象的访问机制及其他
猜你喜欢
Due to the invalidation of the prospectus of bori technology, CICC has stopped providing guidance to it and abandoned the listing on the Hong Kong stock exchange?
Clip: learning transferable visual models from natural language monitoring
[graduation season] role conversion
ML:机器学习工程化之团队十大角色背景、职责、产出物划分之详细攻略
3 - wire SPI Screen Drive
在线文本数字识别列表求和工具
Break through the performance bottleneck of image recognition through rust language computing acceleration technology
idea 插件开发一些异常处理
getReader() has already been called for this request
How to measure the thickness of glass substrate by spectral confocal
随机推荐
XSS notes (Part 2)
Memcached foundation 6
flutter系列之:flutter中的flow
Interface test framework practice (I) | requests and interface request construction
memcached基础
Weibo comments on high performance and high availability architecture
ESP32-添加多目录的自定义组件
XSS笔记(下)
Operating instructions and Q & A of cec-i China learning machine
Object access mechanism and others
George Washington University: Hanhan Zhou | PAC: auxiliary value factor decomposition with counterfactual prediction in Multi-Agent Reinforcement Learning
3-wire SPI screen driving mode
Structure the fifth operation of the actual camp module
Encapsulation of unified result set
memcached基础1
Online text digit recognition list summation tool
Keepalived 实现 Redis AutoFailover (RedisHA)12
LeetCode 142. Circular linked list II
Ymal文件的增删改查
memcached基础3