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 的一个子树包括 ...
随机推荐
- HDU_2112(最短路)
经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...
- Android.StructureOfAndroidSourceCodeRootTree
Refference 1. How to understand the directory structure of android root tree? http://stackoverflow.c ...
- 第一个ASP.NET Web API (C#)程序
本文翻自http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api 绝对手工制作,如有雷同,实属巧合. 转载请注明. ...
- VS2010正则批量替换set_和get_
批量替换set_: daohang.set_ChannelName(rowArray[0]["ChannelName"].ToString()); daohang.set_Chan ...
- Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils
Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils Spring 系列目录(https://www.cnblogs.com/binary ...
- 论坛:一对一关联映射/单向关联/两个类间,可以有两个(多个)关联关系/content为大文本类型/
>>单向:只写一端的映射属性,另一端不写(有一端用不着);双向:两端都写映射属性 >>一对一关联有两类:一类基于主键的(一般不使用),一类基于外键的(重点学习): 外键:是一个 ...
- java调用第三方包的例子
第三方包路径 D:\jp\log4j\log4j-1.2.16.jar 代码D:\jp\log4j\Log4jDemo.java import org.apache.log4j.*; public c ...
- activiti 清库脚本(转)
在使用activiti 的时候会经常遇到需要清空数据库中的数据,因此本文重点讲解如何解决该问题. 再删除数据的时候,需要注意有主外键约束的问题?下面罗列的DDL可以结合自身的业务需求进行灵活改造. D ...
- 【算法专题】工欲善其事必先利其器—— 常用函数和STL
一. 常用函数 #include <stdio.h> int getchar( void ); //读取一个字符, 一般用来去掉无用字符 char *ge ...
- 神奇的幻方(NOIP2015)
先给题目链接:神奇的幻方 太水了这题,直接模拟就行,直接贴代码. #include<bits/stdc++.h> using namespace std; int main(){ int ...