【题目描述】

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 of T1.

Notice: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.

有两个不同大小的二叉树:T1有上百万的节点;T2有好几百的节点。请设计一种算法,判定T2是否为T1的子树。

【注】若 T1 中存在从节点 n 开始的子树与 T2 相同,我们称 T2 是 T1 的子树。也就是说,如果在 T1 节点 n 处将树砍断,砍断的部分将与 T2 完全相同。

【题目链接】

www.lintcode.com/en/problem/subtree/

【题目解析】

判断 T2是否是 T1的子树,首先应该在 T1中找到 T2的根节点,找到根节点后,两棵子树必须完全相同。所以整个思路分为两步走:第一:找根节点,第二:判断两棵树是否全等。看起来很简单,但实际实现时还是细致一点,尤其要注意递归的先后顺序、条件与&条件或的处理。

【参考答案】

www.jiuzhang.com/solutions/subtree/

Lintcode245 Subtree solution 题解的更多相关文章

  1. CF742B Arpa's obvious problem and Mehrdad's terrible solution 题解

    Content 有一个长度为 \(n\) 的数组,请求出使得 \(a_i \oplus a_j=x\) 且 \(i\neq j\) 的数对 \((i,j)\) 的个数.其中 \(\oplus\) 表示 ...

  2. Lintcode360 Sliding Window Median solution 题解

    [题目描述] Given an array of n integer, and a moving window(size k), move the window at each iteration f ...

  3. Lintcode249 Count of Smaller Number before itself solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...

  4. Lintcode248 Count of Smaller Number solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, value from ...

  5. Lintcode247 Segment Tree Query II solution 题解

    [题目描述] For an array, we can build a Segment Tree for it, each node stores an extra attribute count t ...

  6. Lintcode227 Mock Hanoi Tower by Stacks solution 题解

    [题目描述] In the classic problem of Towers of Hanoi, you have 3 towers and N disks of different sizes w ...

  7. Lintcode223 Palindrome Linked List solution 题解

    [题目描述] Implement a function to check if a linked list is a palindrome. 设计一种方式检查一个链表是否为回文链表. [题目链接] w ...

  8. Lintcode221 Add Two Numbers II solution 题解

    [题目描述] You have two numbers represented by a linked list, where each node contains a single digit. T ...

  9. Lintcode212 Space Replacement solution 题解

    [题目描述] Write a method to replace all spaces in a string with%20. The string is given in a characters ...

随机推荐

  1. Tomcat8.5.24日志自动清理(maxDays)功能探究

    前言 测试人员反馈tomcat目录下的日志占用空间很大,需要自动清理.接到这个反馈时,想象着应该是一个很简单的功能,tomcat应该已经实现了日志的自动清理功能.于是乎,我先到网上查询了如何自动清除t ...

  2. 有了Openvswitch和Docker,终于可以做《TCP/IP详解》的实验了!

    所有做过网络编程的程序员,想必都会看<TCP/IP详解>卷一:协议 后来出了第二版,但是由于第一版才是Rechard Stevens的原版,本人还是多次看了第一版. 对这一版印象最深的就是 ...

  3. C语言_简单的阶乘函数

    include <stdio.h> long jc (int num); long jc2 (int num); int main() { long n; n = jc(); printf ...

  4. c++ 如何获取多线程的返回值?

    // Console.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdlib.h> #include ...

  5. python模拟登录浙江大学彩云库

    前言: 群里一位朋友叫我帮他写 一个模拟登录的. 代码: import requests import time url="http://yk3.gokuai.com/web/index&q ...

  6. 嵌入式Linux引导过程之1.6——Xloader的Xloader_Entry

    我们已经看完了XLOADER_ENTRY里调用的前两个标号的代码,分别是sys_init和ddr_init.对于一个嵌入式系统来说,这两 个部分的代码是在一开始就执行的,至少是在从bootrom里面的 ...

  7. Srtuts2实现登录界面(不连接数据库)

    1.设计思路 (1)利用Struts2框架设计出一个登录页面的跳转,当用户名和密码都正确时, 跳到登录成功页面:否则,跳到登录失败页面,并在10秒钟内跳到登录界面: (2)在Action中判断用户名和 ...

  8. Java Web项目(Extjs)报错二

    1.Java Web项目(Extjs)报错二 具体报错如下: usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ...

  9. 解决MyEclipse中的Building workspace问题

    解决MyEclipse中的Building workspace问题 1.方法一 点击"Project",取消勾选"Build Automatically" 2. ...

  10. zTree实现地市县三级级联封装类

    zTree实现地市县三级级联封装类 Province.java: /** * @Title:Province.java * @Package:com.gwtjs.model * @Descriptio ...