【easy】100. Same Tree

判断两棵二叉树是否相等

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if ((p==NULL&&q!=NULL)||(q==NULL&&p!=NULL)) return false;
        if (p==NULL && q==NULL) return true;
        
        if (p->val != q->val) return false;
        else return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right); 
        /*
        if (p->left != NULL && q->left != NULL){
            if (p->left->val != q->left->val) return false;
            isSameTree(p->left,q->left);
        }
        if (p->right != NULL && q->right != NULL){
            if (p->right->val != q->right->val) return false;
            isSameTree(p->right,q->right);
        }
        //一个大错特错的递归…………
        */
        //return true;
    }
};

 

posted @ 2018-01-18 17:13  Sherry_Yang  阅读(117)  评论(0编辑  收藏  举报