标签:problem art tco other iss 节点 暴力 not btree
题目链接:https://leetcode-cn.com/problems/subtree-of-another-tree/
思路:dfs序+暴力匹配
/**
* 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:
vector<int>v;
void LDR(TreeNode* s)
{
if(s->left==NULL)
v.push_back(-1);
else
LDR(s->left);
v.push_back(s->val);
if(s->right==NULL)
v.push_back(-2);
else
LDR(s->right);
}
vector<int>v1;
void LDR1(TreeNode* s)
{
if(s->left==NULL)
v1.push_back(-1);
else
LDR1(s->left);
v1.push_back(s->val);
if(s->right==NULL)
v1.push_back(-2);
else
LDR1(s->right);
}
bool isSubtree(TreeNode* s, TreeNode* t) {
LDR(s);
LDR1(t);
int i=0;
int j=0;
int index=0;
int flag=0;
while(true)
{
if(j==v1.size())
{
flag=1;
break;
}
if(index==v.size()-v1.size()+1)
{
break;
}
if(v[i]==v1[j])
{
i++;
j++;
}
else
{
i=index+1;
index++;
j=0;
}
}
if(flag==1)
{
return true;
}
return false;
}
};
标签:problem art tco other iss 节点 暴力 not btree
原文地址:https://www.cnblogs.com/hang-shao/p/12842020.html