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. 一个基于DPI技术实现了内网资产识别的应用

    https://www.forescout.com/products/counteract/see/visibility-capabilities/ Home ≫ Products ≫ ForeSco ...

  2. [转] initrd详解

    转自:http://www.cnblogs.com/leaven/archive/2010/01/07/1641324.html 在Linux操作系统中,有一项特殊的功能——初始化内存盘INITRD( ...

  3. iOS8 UIAlertView键盘闪一下的问题

    if (SYSTEM_VERSION >= 8.0) { UIAlertController *alertCtrl = [UIAlertController alertControllerWit ...

  4. for 循环分解

    for (expression1; expression2; expression3) { statement; } statement称为循环体 expression1为初始化部分,只在循环开始前执 ...

  5. Null value was assigned to a property of primitive type setter of cn.itcast.oa.domain.Forum.topicCount

    [引用http://m.blog.csdn.net/blog/u013998070/41087351] Null value was assigned to a property of primiti ...

  6. ExportGrid Aspose.Cells.dll

    using Aspose.Cells; using Aspose.Words; using System; using System.Collections; using System.Collect ...

  7. Android NDK定位.so文件crash代码位置

    参考:http://blog.csdn.net/xyang81/article/details/42319789 问题:      QRD8926_110202平台的Browser必现报错.(去年的项 ...

  8. java多线程知识点

    下面是我学习多线程记录的知识点,并没详细讲解每个知识点,只是将重要的知识点记录下来,有时间可以看看,如果有不对的地方,欢迎大家指出,谢谢! 1.多线程的状态和创建方式:     线程的状态:      ...

  9. Mac版Java安装与配置

    一.下载并安装JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 双击下载的 ...

  10. canvas 实现贪吃蛇游戏

    var canvas = document.getElementById('canvas'); var cxt = canvas.getContext('2d'); // 定时器 var timer; ...