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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【leetcode】572. Subtree of Another Tree

    题目如下: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...

  4. 【easy】572. Subtree of Another Tree

    判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二 ...

  5. 572. Subtree of Another Tree

    Problem statement: Given two non-empty binary trees s and t, check whether tree t has exactly the sa ...

  6. 572. Subtree of Another Tree 大树里包括小树

    [抄题]: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...

  7. [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 ...

  8. [leetcode] 543. Diameter of Binary Tree (easy)

    原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...

  9. LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40

    572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 ...

随机推荐

  1. taglib简介

    在 JSP最佳实践的 上一期,您学习了一种基于scriptlet的技术,这种技术被用来将上次修改的时间戳添加到JavaServer Page(JSP)文件中.不幸的是,比起它所提供的短期利益,scri ...

  2. linux 输出重定向

    输出重定向 标准输入 文件描述符:0 设备:键盘 设备文件名:/dev/stdin 标准输出 文件描述符:1 设备:显示器 设备文件名:/dev/sdtout 标准输出重定向 命令 >> ...

  3. js对象(BOM部分/DOM部分)

    JS总体包括ECMAScript,DOM,BOM三个部分,但是能够和浏览器进行交互的只有DOM和BOM,那么到底什么是DOM和BOM呢 概念 BOM(Browser Object Model)是指浏览 ...

  4. linux学习第三天 (Linux就该这么学)

    今天是学习的第三天,讲了很多命令,又赶上双11,网络经常波动,我经常掉线,没有听到多少,回头再看一下录播.我也写一下讲的命令吧,也加深一下命令的印象.第三章老师讲完了. ifconfig命令:输出信息 ...

  5. samtools

    samtools 用法 samtools <command> [options] command 见以下列表, 每个 command 的 options 也不同 dict faidx in ...

  6. Spring整合jedis 集群模式

    引入jedis依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...

  7. NOIP训练测试3(2017081601)

    上一波题还是比较水的吧?[?????] 也许吧! 但时间还是比较紧的,所以我从2.5个小时延长至3个小时了. 不管了,做题不能停,今天继续测试. 水不水自己看,我什么也不说(zhe shi zui h ...

  8. JS高级-String-正则表达式:

    1. String: 由多个字符组成的字符只读数组 vs 数组: 相同: 1. 下标, 2. .length, 3. 遍历, 4. .slice 不同: 类型不同!  API不通用 API: 所有字符 ...

  9. Linux运维之docker虚拟化部署nginx

    一.Docker的概念 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱 ...

  10. unity3d的执行顺序