【题目描述】

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. Asp.Net Core2.0 基于QuartzNet任务管理系统

    Quartz.NET官网地址:https://www.quartz-scheduler.net/ Quartz.NET文档地址:https://www.quartz-scheduler.net/doc ...

  2. shell脚本比较字符串相等

    昨天在定位一个bug的时候,需要些shell脚本,用到了字符串的比较,出了一个小问题,就搜索总结了一下. 第一种方法:-eq 对比字符串是否相等,我首先想到的就是 -eq,然而打印的结果不正确.各种e ...

  3. uva1354 枚举二叉树

    这题很难,这几天一直在想这题,最后看了汝佳大哥的代码才明白.贴上代码 // UVa1354 Mobile Computing // Rujia Liu #include<cstdio> # ...

  4. 聊聊JavaScript-闭包

    今天聊聊闭包,网上五花八门的定义和解释很多很多,是不是搞得你很懵逼:每次看闭包,都不同,本来自己懂,看完别人的之后就开始怀疑自己了.在我看来,闭包简单的说就是函数里面套函数,再往大了说就是我函数外面想 ...

  5. Selenium里可以自行封装与get_attribute对应的set_attribute方法

    我们在做UI自动化测试的过程中,某些情况会遇到,需要操作WebElement属性的情况. 假设现在我们需要获取一个元素的title属性,我们可以先找到这个元素,然后利用get_attribute方法获 ...

  6. php基本函数的学习(2)

    chgrp chgrp — 改变文件所属的组 说明: bool chgrp ( string $filename , mixed $group ) 尝试将文件 filename 所属的组改成 grou ...

  7. Swift 之属性setter、getter方法

    Swift 之属性setter.getter方法 Swift中的属性分为两种属性,一种就是计算型属性 一种就是存储型属性,开始我虽然知道这两种属性,但是了解并不深对于他的setter和getter方法 ...

  8. 关于VS2013调试IIS应用源代码时无法进入断点的问题总结

    调试无法进入断点 前言:今天再次遇到之前调试无法进入断点的问题,本来想不写呢觉得没什么只是又犯了同样的错误,但是我发现这个问题我分析起来还是挺费劲的,我仔细想了想原因, 是因为自己对之前的错误没有进行 ...

  9. Android的OkHttp开源框架的使用方法

    前段时间研究了下Android里面非常火爆的网络请求库OkHttp,这篇文章主要来介绍下OkHttp的常用请求的使用方式,后面一篇文章会介绍本人基于OkHttp封装的一个操作更简单.更适用于项目的网络 ...

  10. linux驱动---字符设备的注册register_chrdev说起

    首先我们在注册函数里面调用了register_chrdev(MEM_MAJOR,"mem",&memory_fops),向内核注册了一个字符设备. 第一个参数是主设备号,0 ...