题目:572. 另一棵树的子树
思路:对于二叉树root,只要当前的值等于二叉树subRoot的值,我们就可以使用深度优先搜索dfs2进行比对。比对成功直接输出true,失败的话,就用二叉树root的左右子树再与二叉树subRoot进行比对。直到比对成功,或当前的二叉树root为nullptr,即失败。细节看注释
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool dfs2(TreeNode* root, TreeNode* subRoot