当前位置:网站首页>[Li Kou] a subtree of another tree
[Li Kou] a subtree of another tree
2022-07-19 06:26:00 【Patrick star`】
572. The subtree of another tree - Power button (LeetCode)
subject :
Here are two binary trees root and subRoot . test root Whether and subRoot Subtrees with the same structure and node values . If there is , return true ; otherwise , return false .
Binary tree tree A subtree of includes tree A node of and all descendants of this node .tree It can also be seen as a subtree of its own .
Ideas :

Traversal by preorder , When you meet with in the tree subRoot When the value of the hotel is equal , Judge whether it is the same tree , If yes, then subRoot It's a subtree of another tree
Code :
bool isSameTree(struct TreeNode* root, struct TreeNode* subRoot)
{
if(root==NULL && subRoot == NULL)
{
return true;
}
if(root==NULL)
{
return false;
}
if(subRoot == NULL)
{
return false;
}
if(root->val != subRoot->val)
{
return false;
}
bool ret1 = isSameTree(root->left,subRoot->left);
bool ret2 = isSameTree(root->right,subRoot->right);
return ret1&&ret2;
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot)
{
if(root == NULL && subRoot == NULL)
{
return true;
}
if(root == NULL)
{
return false;
}
if(root->val == subRoot->val)// See whether it is the same subtree when the values are equal
{
if(isSameTree(root,subRoot))
{
return true;
}
}
bool ret1 = isSubtree(root->left,subRoot);
bool ret2 = isSubtree(root->right,subRoot);
return ret1||ret2;
}边栏推荐
- 2021-09-15
- 量子三体问题: 数值计算概述
- c语言调用文件浏览器,实现选择文件的效果
- TypeScript学习
- 2021 - 09 - 15
- Darwin reflex summary
- Vscode instant English translation plug-in [translation (English Chinese Dictionary)]
- Internship written examination answers
- Positional Change of the Eyeball During Eye Movements: Evidence of Translatory Movement眼球运动过程中眼球的位
- Acwing game 58 (AK)
猜你喜欢
随机推荐
Acwing game 59 (AK)
EOG based eye movement detection and gaze estimation for an asynchronous virtual keyboard
2022 robocom world robot developer competition - undergraduate group (provincial competition)
Open source online markdown editor -- [editor.md]
RestAPI实现聚合(黑马教程)
Acwing第58场周赛(AK)
Markdown语法和常用快捷键
Solve cannot read properties of null (reading 'pickalgorithm')
Acwing daily question three thousand five hundred and eleven
filezilla传输虚拟机速度慢解决方法
Computational geometry (2)
【力扣】用栈实现队列
Make config analysis of configuration commands before uboot compilation
索引库中的文档的操作
Creation and implementation of WebService interface
Interview review nth time
Leetcode string
QT creator flashback solution
浅谈跨域的几种解决方案
Résoudre le problème de l'ambiguïté de la cible dans l'interaction de fixation 3D par l'estimation de la profondeur vor







![Open source online markdown editor -- [editor.md]](/img/f3/b37acf934aa2526d99c8f585b6f229.png)
