相同的树
 

 
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 isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
    return 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);
}
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);
}
 
翻转二叉树
 

 
struct TreeNode* invertTree(struct TreeNode* root){
    if(root==NULL) return NULL;
    
    
    
    
    invertTree(root->left);
    invertTree(root->right);
    struct TreeNode* tem=root->left;
    root->left=root->right;
    root->right=tem;
    return root;
}
 
对称二叉树
 

 
 bool ismirror(struct TreeNode* left,struct TreeNode* right)
 {
     if(left==NULL&&right==NULL) return true;
     if(left==NULL||right==NULL) return false;
     if(left->val!=right->val) return false;
     return ismirror(left->left,right->right)&&ismirror(left->right,right->left);
 }
 
bool isSymmetric(struct TreeNode* root){
    if(root==NULL) return true;
    if(root->left==NULL&&root->right==NULL) return true;
    if(root->left==NULL||root->right==NULL) return false;
    
    return ismirror(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* cnt,int* nums)
{
    
    if(root==NULL) return ;
    nums[(*cnt)++]=root->val;
    
    
    
    preorder(root->left,cnt,nums),preorder(root->right,cnt,nums);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
    int n=TreeSize(root);
    int* nums=(int*)malloc(sizeof(int)*n);
    int cnt=0;
    preorder(root,&cnt,nums);
    *returnSize=n;
    return nums;
}