15.Subtree of Another Tree(判断一棵树是否为另一颗树的子树)
Level:
Easy
题目描述:
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself.
Example 1:
Given tree s:
3
/ \
4 5
/ \
1 2
Given tree t:
4
/ \
1 2
Return true, because t has the same structure and node values with a subtree of s.
Example 2:
Given tree s:
3
/ \
4 5
/ \
1 2
/
0
Given tree t:
4
/ \
1 2
Return false
思路分析:
判断t树是否为s树的子树,先在s中找到和t根节点相同的节点,然后从该节点出发判断是否存在与t完全相同的结构。
代码:
public class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x){
val=x;
}
}
public class Solution{
public boolean isSubtree(TreeNode s,TreeNode t){
if(s==null||t==null)
return false;
return checkTree(s,t)||isSubtree(s.left,t)||isSubtree(s.right,t);//找到与t根节点相同的节点,然后开始进行判断
}
public boolean checkTree(TreeNode s,TreeNode t){
if(s==null&&t==null) //同时为null证明结构一样
return true;
if(s==null||t==null) //如果任意一个不为空,代表结构不一样
return false;
return (s.val==t.val)&&checkTree(s.left,t.left)&&checkTree(s.right,t.right);//这是判断结构是否相同的条件,对应节点值相同,并且左右子树对应的结构也要相同。
}
}
15.Subtree of Another Tree(判断一棵树是否为另一颗树的子树)的更多相关文章
- [LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树
将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树. 用list记录所有子树的序列化,和目标树比较. List<String> list = new ArrayList< ...
- 南昌网络赛 Distance on the tree 主席树+树剖 (给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数。)
https://nanti.jisuanke.com/t/38229 题目: 给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数. #include <bits/stdc++.h ...
- LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- 【easy】572. Subtree of Another Tree
判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二 ...
- [LeetCode] Subtree of Another Tree 另一个树的子树
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
- 【LeetCode】572. 另一个树的子树 Subtree of Another Tree(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 方法二:DFS + DFS 方法三 ...
- [LeetCode] Same Tree 判断相同树
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- LeetCode 572. Subtree of Another Tree (是否是另一个树的子树)
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- Git学习笔记(四)标签和搭建Git服务
一.标签是什么 发布一个版本时,我们通常先在版本库中打一个标签,这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一 ...
- Apache Continuum 远程命令执行漏洞
这个是apache现在不维护的服务了. 服务使用了struts2框架,目前测试是使用的最新版.
- OpenGL渲染流水线
其实OpenGL的流水线,对我学习来说只能算是一个概念性的东西.毕竟OpenGL也在发展,流水线也不会是一成不变的. 不过理解流水线的过程,重点在于理解每一步的作用,进而可以如何衔接起来,完成整个绘制 ...
- Android 使用SWIG生成Jni代码<转>
http://blog.csdn.net/u010780612/article/details/51066819 感觉也就那样把,我没有编译成功,实用性暂时没看到多大优点...
- Java知识点总结1
1.java的引用传递和值传递 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递? 答:是值传递.Java 编程语言只有值传递参 ...
- floyd路径记录
#include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #in ...
- 离散对数的求解(bsgs)
bsgs算法 主要用来解决${A^x} = B(\bmod C)$(c是质数),都是整数,已知A.B.C求x. 例:poj 2417 Discrete Logging 具体步骤如下: 先把$x = i ...
- SQL语句兼容性规范
一.DDL兼容性规范(防止表结构变更后,原有的SQL执行报错)只能增加字段或修改字段长度(字段长度改大),不能修改字段名字和类型,不能删除字段不能删除表或者修改表名称 二.DML兼容性规范insert ...
- javascript 函数,事件
1.函数字符串函数 var s=new string(); var ss="hello world"; var sss=""HELLO, WORLD" ...
- 32-回文字符串(dp)
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=37 回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描 ...