1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
class Solution { public: bool isSubStructure(TreeNode* A, TreeNode* B) { return (A != nullptr && B != nullptr) && (dfs(A, B) || isSubStructure(A->left, B) || isSubStructure(A->right, B)); }
private:
bool dfs(TreeNode* A, TreeNode* B) { if(B == nullptr) return true; if(A == nullptr || A->val != B->val) return false; return dfs(A->left, B->left) && dfs(A->right, B->right); }
};
|
https://leetcode.cn/problems/shu-de-zi-jie-gou-lcof/