当前位置:网站首页>二叉树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;
}
边栏推荐
- Visual introduction to Matplotlib and plotnine
- flutter系列之:flutter中的flow
- buuctf-pwn write-ups (6)
- Law of Large Numbers
- get_sequencer的用法总结
- George Washington University: Hanhan Zhou | PAC: auxiliary value factor decomposition with counterfactual prediction in Multi-Agent Reinforcement Learning
- memcached基础
- UVM in UVM_ report_ Enabled usage
- memcached基础5
- idea 插件开发一些异常处理
猜你喜欢

Topolvm: kubernetes local persistence scheme based on LVM, capacity aware, dynamically create PV, and easily use local disk

浏览器缓存
![[graduation season] role conversion](/img/4e/aa763455da974d9576a31568fc6625.jpg)
[graduation season] role conversion

The most difficult 618 in history, TCL won the first place in both jd.com and tmall.com shares in the TV industry

自定义类加载器对类加密解密

Structure the fifth operation of the actual camp module

Two days of beautiful butterfly animation

Flutter series: flow in flutter

How to measure the thickness of glass substrate by spectral confocal

架构实战营模块五作业
随机推荐
leetcode 1143. Longest Commom Subsequence 最长公共子序列(中等)
Systematic analysis of social networks using Networkx: Facebook network analysis case
Recursion will make strtok more attractive
Memcached foundation 3
memcached基础1
flutter系列之:flutter中的flow
IIS 部署静态网站和 FTP 服务
Solve the problem that only one line of text is displayed or not displayed in u8glib
Meituan: data management and pit avoidance strategy summarized after stepping on Thunder for several years
可视化介绍 Matplotlib 和 Plotnine
Amazon ElastiCache 飞速搭建缓存服务集群,这才叫快
Did your case really pass?
LeetCode 142. Circular linked list II
TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
memcached基础6
自定义类加载器对类加密解密
XSS笔记(下)
在连接数据库的时候遇到了点问题,请问怎么解决呀?
Summary of working at home during the epidemic | community essay solicitation
福元医药上市在即:募资净额将达到16亿元,胡柏藩为实际控制人