bool isUnivalTree(struct TreeNode* root){
if(root==NULL)
return true;
if(root->left&&root->val!=root->left->val)
return false;
if(root->right&&root->val!=root->right->val)
return false;
return isUnivalTree(root->left)
&& isUnivalTree(root->right);
}
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 Symmetric(struct TreeNode* L,struct TreeNode* R)
{
if(R==NULL&&L==NULL)
return true;
if(R==NULL||L==NULL||R->val!=L->val)
return false;
return Symmetric(R->left,L->right)&&Symmetric(L->left,R->right);
}
bool isSymmetric(struct TreeNode* root){
if (root==NULL)
return false;
return Symmetric(root->left,root->right);
}
int TreeSize(struct TreeNode* root)
{
if(root==NULL)
return 0;
return TreeSize(root->left)+TreeSize(root->right)+1;
}
void preorder(struct TreeNode* root,int* a,int* pi)
{
if(root==NULL)
return;
a[*pi]=root->val;
printf("a[%d]=%d\n",*pi,a[*pi]);
(*pi)++;
preorder(root->left,a,pi);
preorder(root->right,a,pi);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int n=TreeSize(root);
printf("size:%d\n",n);
int* a=(int*)malloc(sizeof(int)*n);
int i=0;
preorder(root,a,&i);
*returnSize=n;
return a;
}
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(isSameTree(root,subRoot))
return true;
return isSubtree(root->left,subRoot)
||isSubtree(root->right,subRoot);
}
#include <stdio.h>
#include <stdlib.h>
typedef char BTDataType;
typedef struct BinaeyTreeNode
{
BTDataType data;
struct BinaeyTreeNode* left;
struct BinaeyTreeNode* right;
}BTNode;
BTNode* BinaeyTreeCreate(BTDataType* a,int* pi)
{
if(a[*pi]=='#')
{
(*pi)++;
return NULL;
}
BTNode* root=(BTNode*)malloc(sizeof(BTNode));
if(root==NULL)
{
perror("malloc fail");
return NULL;
}
root->data=a[*pi];
(*pi)++;
root->left=BinaeyTreeCreate(a,pi);
root->right=BinaeyTreeCreate(a,pi);
return root;
}
void Inorder(BTNode* root)
{
if(root==NULL)
return;
Inorder(root->left);
printf("%c ",root->data);
Inorder(root->right);
}
int main()
{
char str[100];
scanf("%s",str);
int i=0;
BTNode* root=BinaeyTreeCreate(str,&i);
Inorder(root);
return 0;
}
9.翻转二叉树
struct TreeNode* invertTree(struct TreeNode* root){
if(root==NULL)
return root;
struct TreeNode* tmp=root->left;
root->left=root->right;
root->right=tmp;
invertTree(root->left);
invertTree(root->right);
return root;
}
10.判断一棵二叉树是否是平衡二叉树
int TreeHeight(struct TreeNode* root)
{
if(root==NULL)
return 0 ;
int lh=TreeHeight(root->left);
int rh=TreeHeight(root->right);
return lh>rh?lh+1:rh+1;
}
bool isBalanced(struct TreeNode* root){
if(root==NULL||(root->left==NULL)&&(root->right==NULL))
return true;
if(fabs(TreeHeight(root->left)-TreeHeight(root->right))>1)
return false;
return isBalanced(root->left)&&isBalanced(root->right);
}