Leetcode--572. Subtree of Another Tree(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.
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if(s==null)
return false;
if(judge(s,t))
return true;
return isSubtree(s.left,t)||isSubtree(s.right,t);
}
public boolean judge(TreeNode s,TreeNode t){
if(s==null||t==null)
return s==t;
if(s.val!=t.val)
return false;
return judge(s.left,t.left)&&judge(s.right,t.right);
}
}
Leetcode--572. Subtree of Another Tree(easy)的更多相关文章
- 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 ...
- 572. Subtree of Another Tree(easy)
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
题目如下: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
- 【easy】572. Subtree of Another Tree
判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二 ...
- 572. Subtree of Another Tree
Problem statement: Given two non-empty binary trees s and t, check whether tree t has exactly the sa ...
- 572. Subtree of Another Tree 大树里包括小树
[抄题]: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
- [LC] 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] 543. Diameter of Binary Tree (easy)
原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...
- LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40
572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 ...
随机推荐
- msyql [note] mysqld (mysqld 5.6.40) starting as process xxxx...
my.ini有2个配置一个是客户端[client],一个是服务器端[mysqld] 如果把2个都设置为utf8,那么中文会显示乱码,或者无法插入中文 而把客户端设置为gbk,把服务器端设置为utf8就 ...
- 【Linux】关于路由跟踪指令traceroute
稍有计算机常识的人都知道ping命令,是用来检查自己的主机是否与目标地址接通,自己的主机与目标地址的通讯包通讯速率,所谓的通讯包也就是那些什么TCP/IP,UDP包,这里说得通俗一点,比如,就拿这 ...
- iOS.UITableView.SectionIndex
1. 为tableview中section建立索引来加速tableview的滚动. http://nshipster.com/uilocalizedindexedcollation/ 2. 获取汉字的 ...
- Bad owner or permissions on $HOME/.ssh/config
摘自:https://www.cnblogs.com/ytjjyy/p/4076442.html The ssh with RHEL 4 is a lot more anal about securi ...
- Eclipse快速生成覆盖方法、Getter、Setter的方法
点击鼠标右键 --> Source --> 直接使用快捷键 Alt+Shift+s
- 摹客 · Veer 第二届设计大赛邀你来战!
2018年12月,摹客设计大赛一年一度一归来. 继2017年摹客全国首届原型设计大赛成功举办后,本次大赛是摹客第二届设计大赛.大赛由摹客主办,Veer独家冠名赞助,iSlide和创客贴协办,国内多家知 ...
- Gazebo: Could not find parameter robot_description on parameter server
robot_state_publisher looks for the parameter "robot_description" by default. The robot_st ...
- SpringMVC学习笔记:数据的接收与返回
SpringMVC的定义:Spring Web MVC is the original web framework built on the Servlet API and included in t ...
- 将Struts放入spring框架中
第一步:写好ACtion package com.inspur.actions; import javax.servlet.http.HttpServletRequest;import javax.s ...
- SpringMVC 学习 八 SSM环境搭建(一) web.xml配置
第一步:导入jar包 注意包的兼容性,以后采用maven会好很多 第二步:配置web.xml 在web.xml中,主要的配置内容有以下几点 (1)spring容器配置文件的位置 <!-- spr ...