目录
前言
前面我们学习过了二叉树的相关知识点,那么今天我们就做做练习,下面我会介绍几道关于二叉树的leetcode习题,我们一起来看看吧!
1. 二叉树的中序遍历
代码实现:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
void travel(struct TreeNode* root, int*num,int*returnSize){
if(!root)
return;
travel(root->left,num,returnSize);
num[*returnSize]=root->val;
*returnSize+=1;
travel(root->right,num,returnSize);
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int* num=(int*)malloc(sizeof(int)*100);
*returnSize=0;
if(!root)
return NULL;
travel(root,num,returnSize);
return num;
}
2. 相同的树
代码实现:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if(!p&&!q)
return true;
if(!p||!q)
return false;
if(p->val!=q->val)
return false;
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
3. 二叉树的最大深度
代码实现:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxDepth(struct TreeNode* root){
if(!root){
return 0;
}
int l=maxDepth(root->left);
int r=maxDepth(root->right);
if(l>r)
return l+1;
else
return r+1;
}
4. 二叉树的最小深度
代码实现:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int minDepth(struct TreeNode* root){
if(!root){
return 0;
}
else if(!root->left&&root->right)
return minDepth(root->right)+1;
else if(!root->right&&root->left)
return minDepth(root->left)+1;
else {
int l=minDepth(root->left);
int r= minDepth(root->right);
if(l>r)
return r+1;
else
return l+1;
}
}
5.二叉树的前序遍历
代码实现:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
void travel(struct TreeNode* root, int *num,int* returnSize)
{
if(!root)
return;
num[*returnSize]=root->val;
*returnSize+=1;
travel(root->left,num,returnSize);
travel(root->right,num,returnSize);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int* num=(int*)malloc(sizeof(int)*100);
*returnSize=0;
travel(root,num,returnSize);
return num;
}
6. 二叉树的后序遍历
代码实现:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
void travel(struct TreeNode* root, int *num,int* returnSize)
{
if(!root)
return;
travel(root->left,num,returnSize);
travel(root->right,num,returnSize);
num[*returnSize]=root->val;
*returnSize+=1;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize){
int *num=(int*)malloc(sizeof(int)*500);
*returnSize=0;
travel(root,num,returnSize);
return num;
}
7. 对称二叉树
代码实现:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool check(struct TreeNode* p,struct TreeNode* q){
if(!p&&!q)
return true;
if(p==NULL||!q)
return false;
if(p->val!=q->val)
return false;
else
return check(p->left,q->right)&&check(p->right,q->left);
}
bool isSymmetric(struct TreeNode* root){
return check(root,root);
}
好了,以上就是本期习题的全部内容了,我们下一期再见!
分享一张壁纸: