0 ms C solution


  • 1
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
        if(NULL == p && NULL == q) return true;
        if(NULL != p && NULL != q) return isSameTree(p->left, q->left) && isSameTree(p->right, q->right) && (p->val == q->val);
        return false;
    }
    

  • 0
    V

    Dam lol nice code compression. My solution was the same logical checks:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
        bool retVal = true;
        if (p == NULL && q == NULL) return true;
        if (p == NULL && q != NULL) return false;
        if (p != NULL && q == NULL) return false;
        retVal = isSameTree(p->left, q->left);
        if (!retVal) return false;
        if (p->val != q->val) return false;
        retVal = isSameTree(p->right, q->right);
        if (!retVal) return false;
        return true;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.