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 的一个子树包括 ...
随机推荐
- 网页 PHP 动态师范
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Github上删除仓库
1.先进入到工程里面,然后选择“Settings” 2.将页面拉到最下面,然后点击“Delete this repository”,接着在弹出的窗口中输入需要删除的仓库名. 弹出的窗口如下:
- python 截取某一天的日志,简单操作
#!/usr/bin/python #Filename: Segmentation_log.py import re,sys def openfile(*args): try: f=open(args ...
- python httplib2应用get post
import httplib2,time #装饰器方法,用于记录方法消耗时间 #推荐将print 改成log def timer(func): def _warpper(self,*argv) ...
- readv writev示例程序
当 readv() 时候,需要程序自己提供space,接收数据. #include <stdio.h> #include <stdlib.h> #include <str ...
- 请简要介绍Sping MVC、IoC和AOP
Sping MVC是在Spring框架上发展起来的框架,它提供了构建Web应用程序的全功能MVC模块,使用了Spring可插入的MVC架构,可以自由的选择各个模块所使用的架构,非常灵活.Spring ...
- DB2有五种约束
DB2有五种约束: NOT NULL 约束是这样一种规则,它防止在表的一列或多列中输入空值. 唯一约束(也称为唯一键约束)是这样一种规则,它禁止表的一列或多列中出现重复值.唯一键和主键是受支持的唯一约 ...
- ueditor 上传图片
ueditor在配置图片,附件上传 首先,是以web项目为基础的,需要安装好eclipse以及tomcat 其次,需要下载ueditor(可去百度官网下载 http://ueditor.baidu. ...
- ServiceDesk Plus 服务管理自动指派工单功能
- Python开课复习-10/17
pickle是一个用来序列化的模块序列化是什么?指的是将内存中的数据结构转化为一种中间格式 并存储到硬盘上 反序列化?将硬盘上存储的中间格式数据在还原为内存中的数据结构 为什么要序列化?就是为了将数据 ...