Simple Recursive Solution


  • 0
    P
        bool isSameTree(TreeNode* root1, TreeNode* root2) {
            if(root1==nullptr && root2==nullptr){
                return true;
            }
            else if(root1==nullptr || root2==nullptr){
                return false;
            }
            else{
                return ( (root1->val==root2->val) && isSameTree(root1->left,root2->left) &&  isSameTree(root1->right,root2->right) );
            }
        }
    

Log in to reply
 

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