原题链接在这里:http://www.lintcode.com/en/problem/subtree/

You have two every large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree ofT1.

Have you met this question in a real interview?

Yes
Example

T2 is a subtree of T1 in the following case:

       1                3
/ \ /
T1 = 2 3 T2 = 4
/
4

T2 isn't a subtree of T1 in the following case:

       1               3
/ \ \
T1 = 2 3 T2 = 4
/
4
Note

A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of n is identical to T2. That is, if you cut off the tree at node n, the two trees would be identical.

Time Complexity: O(m*n), m是T1的node数, n 是T2的node 数. Space: O(logm). isSame用logn, isSubtree用logm.

AC Java:

 /**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param T1, T2: The roots of binary tree.
* @return: True if T2 is a subtree of T1, or false.
*/
public boolean isSubtree(TreeNode T1, TreeNode T2) {
// write your code here
if(T2 == null){
return true;
}
if(T1 == null){
return false;
}
return isSame(T1,T2) || isSubtree(T1.left, T2) || isSubtree(T1.right, T2);
} private boolean isSame(TreeNode T1, TreeNode T2){
if(T1 == null && T2 == null){
return true;
}
if(T1 == null || T2 == null){
return false;
}
if(T1.val != T2.val){
return false;
}
return isSame(T1.left, T2.left) && isSame(T1.right, T2.right);
}
}

LintCode Subtree的更多相关文章

  1. lintcode:Subtree 子树

    题目: 子树 有两个不同大小的二叉树: T1 有上百万的节点: T2 有好几百的节点.请设计一种算法,判定 T2 是否为 T1的子树. 样例 下面的例子中 T2 是 T1 的子树: 1 3 / \ / ...

  2. 245. Subtree【LintCode java】

    Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  5. Lintcode245 Subtree solution 题解

    [题目描述] You have two every large binary trees:T1, with millions of nodes, and T2, with hundreds of no ...

  6. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  7. LintCode题解之子树

    思路: 最简单的方法,依次遍历比较就可以了. AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int va ...

  8. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  9. lintcode算法周竞赛

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

随机推荐

  1. 利用Python的三元表达式解决Odoo中工资条中城镇、农村保险的问题

    Python中没有像C#中有三元表达式 A?B:C 但在python中可以通过 A if condition else B 的方式来达到同样的效果. 例如 : 1 if True else 0 输出 ...

  2. JS让input按钮不能点击

    <input value="开通" type="button" id="tijiao" class="button" ...

  3. smarty模板中引用常量没效果

    在thinkphp框架中使用Thinkph的模板引擎,css,js等都没问题,配置为smarty模板引擎后没效果,如图 代码为,$Smarty的s是大写,此时页面不能引入常量,改为小写后正常了. &l ...

  4. include动作标记和include指令标记学习笔记

    我的jsp学习参考书是耿祥义,张跃平编著的jsp大学使用教程这本书,我也向大家推荐这本书,我觉得这本书适合我的学习方式,知识的讲解透彻易懂. include指令标记                   ...

  5. 一种少见的跨目录写webshell方法

    http://hi.baidu.com/kwthqquszlbhkyd/item/480716204cfa33c3a5275afa

  6. Wilddog - 野狗常用知识点

    https://www.wilddog.com/examples/chat-var1 https://z.wilddog.com/web/quickstart 增加或者修改替换整条数据(注意,upda ...

  7. Phaser中很多对象都有一个anchor属性

    游戏要用到的一些图片.声音等资源都需要提前加载,有时候如果资源很多,就有必要做一个资源加载进度的页面,提高用户等待的耐心.这里我们用一个state来实现它,命名为preload. 因为资源加载进度条需 ...

  8. C#中将ListView中数据导出到Excel

    首先 你需要添加引用Microsoft Excel 11.0 Object Library 添加方法:选择项目->引用->右击“添加引用”->选择COM 找到上面组件—>点击“ ...

  9. MyEclipse的注册过程

    说在前面的话: 说到收费软件MyEclipse,大家可能对它又爱又恨,其实软件收钱也是为了有更好的发展,我们的建议是先试用,如果觉得不错,可以使用正版软件! 准备工作: 1.MyEclipse安装文件 ...

  10. Linux 计划任务 Crontab 笔记与总结(5)crontab 常见错误与案例

    ① 环境变量 cd ~ pwd 就会到你登陆的那个用户的根目录下 ls -a 能够查看到 .bash_profile 这个文件 vim .bash_profile 这里面设置了一些环境变量. 可以设置 ...