#力扣 LeetCode572. 另一个树的子树 @FDDLC
发布日期:2021-06-30 21:01:41
浏览次数:2
分类:技术文章
本文共 1008 字,大约阅读时间需要 3 分钟。
题目描述:
Java代码:
class Solution { //两个非空二叉树 s 和 t,s 也可以看做它自身的一棵子树。 public boolean cmp(TreeNode a,TreeNode b){ if(null==a&&null==b)return true; if(null==a^null==b)return false; return a.val==b.val&&cmp(a.left,b.left)&&cmp(a.right,b.right); } public boolean isSubtree(TreeNode s, TreeNode t) { if(s.val==t.val&&cmp(s,t))return true; //s.val==t.val可无 if(s.left!=null&&isSubtree(s.left,t))return true; if(s.right!=null&&isSubtree(s.right,t))return true; return false; }}
补充:
class Solution { //两个非空二叉树 s 和 t,s 也可以看做它自身的一棵子树。 public boolean cmp(TreeNode a,TreeNode b){ if(null==a&&null==b)return true; if(null==a^null==b)return false; return a.val==b.val&&cmp(a.left,b.left)&&cmp(a.right,b.right); } public boolean isSubtree(TreeNode s, TreeNode t) { if(cmp(s,t))return true; if(s.left!=null&&isSubtree(s.left,t))return true; if(s.right!=null&&isSubtree(s.right,t))return true; return false; }}
转载地址:https://liuxingchang.blog.csdn.net/article/details/112132683 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月14日 20时52分38秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
CodeForces - 675A Infinite Sequence(简单数论 细节)
2019-04-30
CodeForces - 1042B Vitamins (思维)
2019-04-30
ACM 2013 长沙区域赛 Alice's Print Service (二分 思维)
2019-04-30
ACM 2013 长沙区域赛 Collision (几何)
2019-04-30
CodeForces - 1064A Make a triangle! (简单模拟)
2019-04-30
51Nod - 1183 编辑距离 (dp)
2019-04-30
ACM 2014 鞍山区域赛 E - Hatsune Miku (dp)
2019-04-30
反向传播&梯度下降 的直观理解程序(numpy)
2019-04-30
ACM 2017 南宁区域赛 Rake it in(对抗搜索)
2019-04-30
CodeForces - 931B World Cup (思维 模拟)
2019-04-30
CodeForces - 996D Suit and Tie (暴力)
2019-04-30
ACM 2017 香港区域赛 E - Base Station Sites(二分)
2019-04-30
ACM 2018 青岛区域赛 J-Books (模拟)
2019-04-30
ACM 2016 沈阳区域赛 E - Counting Cliques (dfs)
2019-04-30
ACM 2017 北京区域赛 J-Pangu and Stones(区间dp)
2019-04-30
HDU - 5643 King's Game (约瑟夫环变式)
2019-04-30
UVA - 1452 Jump (约瑟夫环变式)
2019-04-30
POJ - 3517 And Then There Was One (约瑟夫环变式)
2019-04-30