【题目描述】

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. WordPress 实现附件上传自动重命名但不改变附件标题

    WordPress 上传媒体文件时,默认会保持文件名不变.如果上传文件名中包含中文字符,则会造成部分浏览器显示的文件 URL 疑似乱码甚至无法访问.网上流行较广的是通过注册  wp_handle_up ...

  2. POJ - 1984 Navigation Nightmare 种类并查集

    思路:记录每个点与其根结点的横向距离和纵向距离,当知道其父节点与根结点的关系,很容易推出当前节点与根结点的关系: 直接相加即可. int p = a[x].par; a[x].dx += a[p].d ...

  3. XOR (莫队)

    Time Limit: 2000 ms   Memory Limit: 256 MB Description 给定一个含有n个整数的序列 a1, a2,..., an. 定义 f(x,x) = a[x ...

  4. Django里使用open函数

    Django里使用open函数 前言 在Django里使用open函数打开一个文件的时候,常常会遇到路径错误的问题.我在Django APP里写了一个爬虫用于为网站提供数据,但是需要打开文件,也就是在 ...

  5. js随机出现2个数字

    1和2 随机出现 <script type="text/javascript"> $(function(){ if (Math.random()>0.5) { c ...

  6. Typescript 基础应用

    什么是 TypeScript TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeScri ...

  7. Visual Studio Code搭建python开发环境

    开发Python的环境有很多,原来已经在vs2013上面搭建好python的开发环境了,但是vs2013每次启动都占太多内存(太强大了吧),这下出了vs code,既轻量又酷炫,正好拿来试一试开发py ...

  8. Nginx负载均衡和反向代理的配置和优化

    负载均衡 负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台服务器都具有等价的地位, 反向代理 是指以代理服务器来接受internet上的请求,然后将请求转给内部的服务器 常见的负载均衡 1. ...

  9. Flex中的FusionCharts 2D柱形图

    1.2D柱形图源码 <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:f ...

  10. vxWorks/BootROM Imageq启动顺序详解

    vxWorks/BootROM Imageq启动顺序详解 VxWorks image     分为在ROM中运行和在RAM中运行两种,两者启动顺序的区别在于sysInit()函数的调用,该函数在RAM ...