C++ 0ms


  • 0
    B
        bool isSameTree(TreeNode* p, TreeNode* q) {
        if(p == NULL && q==NULL) return true;
        if(p == NULL || q==NULL || p->val != q->val) return false;
        return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);}

  • 0
    F

    Hi,
    I am wondering the "unbalance tree" case.
    For example a tree only 2 node. A {val=1, left=B, right=NULL}, B(val=1, left=NULL, right=NULL}

    isSameTree(A,A) should return T;
    But your solution will return false due to "if(... || q==NULL || ...) return false;" right?


  • 0
    B

    My solution will return true,it's a recursive program, only judge the node itself eachtime.when leftchild and rightchild are empty at the same time,program will return true first...


Log in to reply
 

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