/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSubStructure(TreeNode* A, TreeNode* B) {
return (A!=nullptr && B!=nullptr) && (recurr(A,B)|| isSubStructure(A->left,B) ||isSubStructure(A->right,B)); //在AB不空的情况下满足这些条件,空了就直接0了
}
bool recurr(TreeNode*A,TreeNode*B){
if(B==nullptr) return true;
if(A==nullptr || A->val!=B->val) return false;
return recurr(A->left,B->left) && recurr(A->right,B->right);
}
};
剑指Offer26:树的子结构-----树查找要想到递归~
最新推荐文章于 2022-05-05 16:38:40 发布