[leetcode] Same Tree

Same Tree

class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        if(!p&&!q){//both are null
            return true;
        }
        if(p&&!q||!p&&q||p->val!=q->val){
            return false;
        }
        bool left=isSameTree(p->left,q->left);//recursion
        bool right=isSameTree(p->right,q->right);
        return left&&right;
    }
};


你可能感兴趣的:([leetcode] Same Tree)