目录
3.二叉树的前序遍历(自己malloc数组并把数据存放进去)
1.单值二叉树
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);
}
总结:二叉树中的判断bool有一套统一公式(本人自己总结):先考虑空树,再考虑反面情况。最后分解成子问题。因为你只有把简单的情况考虑完后,才会进入到复杂的情况,而这种复杂的情况恰恰就是需要被拆解成子问题的情况。
怎么样?是不是有种醉翁之意不在酒的感觉呢?
2.二叉树最大深度
int max(int a,int b)
{
if(a>b)
return a;
return b;
}
int maxDepth(struct TreeNode* root) {
if(root==NULL)
return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
3.二叉树的前序遍历(自己malloc数组并把数据存放进去)
int treesize(struct TreeNode* root)
{
if(root==NULL)
return 0;
return treesize(root->left)+treesize(root->right)+1;
}
void preorderTraversal_(struct TreeNode* root , int* a , int* i)
{
if(root==NULL)
return;
a[(*i)++]=root->val;
preorderTraversal_(root->left,a,i);
preorderTraversal_(root->right,a,i);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
*returnSize =treesize(root);
int* a =(int*)malloc((*returnSize)*sizeof(int));
int i=0;
preorderTraversal_(root,a,&i);
return a;
}
4.检查两颗树是否相同
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);
}
5.对称二叉树
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);
}
bool isSymmetric(struct TreeNode* root) {
return isSymmetric_(root->left,root->right);
}
6.另一颗树的子树
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&&isSameTree(root,subRoot))
return true;
return isSubtree(root->left,subRoot)
||isSubtree(root->right,subRoot);
}
7..二叉树的构建及遍历
#include <stdio.h>
#include <stdlib.h>
typedef struct tree
{
char val;
struct tree* left;
struct tree* right;
}tree;
tree* buildtree(char arr[100],int * pi)
{
if(arr[*pi]=='#')
{
(*pi)++;
return NULL;
}
tree* root =(tree*)malloc(sizeof(tree));
root->val=arr[(*pi)++];
root->left=buildtree(arr,pi);
root->right=buildtree(arr,pi);
return root;
}
void orderby(tree* root)
{
if(root==NULL)
return;
orderby(root->left);
printf("%c ",root->val);
orderby(root->right);
}
int main() {
char arr[101];
scanf("%s",arr);
int i=0;
tree* root=buildtree(arr,&i);
orderby(root);
printf("\n");
return 0;
}
8.翻转二叉树
void swap(struct TreeNode** pa ,struct TreeNode** pb)
{
struct TreeNode* tmp;
tmp=*pa;
*pa=*pb;
*pb=tmp;
}
void invertTree_(struct TreeNode** root1,struct TreeNode** root2)
{
if(*root1==NULL&&*root2==NULL)
return;
if(*root2==NULL)
{
invertTree_(&((*root1)->left),&((*root1)->right));
swap(root1,root2);
return;
}
if(*root1==NULL)
{
invertTree_(&((*root2)->left),&((*root2)->right));
swap(root1,root2);
return;
}
invertTree_(&((*root1)->left),&((*root1)->right));
invertTree_(&((*root2)->left),&((*root2)->right));
swap(root1,root2);
}
struct TreeNode* invertTree(struct TreeNode* root) {
if(root==NULL)
return NULL;
invertTree_(&root->left,&root->right);
return root;
}