算法和数据结构 相同的树

class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        return dfs(p, q);
    }
    
    bool dfs(TreeNode *pt, TreeNode *qt)
    {
        if(pt == NULL && qt == NULL) return true;
        if(pt == NULL && qt != NULL) return false;
        if(pt != NULL && qt == NULL) return false;
        return (pt->val == qt->val) && dfs(pt->left, qt->left) && dfs(pt->right, qt->right); 
    }
};

  

猜你喜欢

转载自www.cnblogs.com/yangwenhuan/p/12448006.html