100. Same Tree

class Solution {
public:
    /**
     * @aaram a, b, the root of binary trees.
     * @return true if they are identical, or false.
     */
    bool isIdentical(TreeNode* a, TreeNode* b) {
        if(a == NULL && b == NULL) return true;
        if(a == NULL ) return false;
        if(b == NULL ) return false;
        return a->val == b->val && isIdentical(a->left,b->left) && isIdentical(a->right,b->right);
    }
};
bool isSameTree(TreeNode *p, TreeNode *q) {
    if (p == NULL || q == NULL) return (p == q);
    return (p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
}

results matching ""

    No results matching ""