【试题描述】定义一个函数,输入判断一个树是否是另一个对的子树

You have two very large binary trees: T1, with millions of nodes, and T2, with hun-dreds of nodes Create an algorithm to decide if T2 is a subtree of T1

Note that the problem here specifies that T1 has millions of nodes—this means that we should be careful of how much space we use Let’s say, for example, T1 has 10 million nodes—this means that the data alone is about 40 mb We could create a string representing the inorder and preorder traversals If T2’s preorder traversal is a substring of T1’s preorder traversal, and T2’s inorder traversal is a substring of T1’s inorder traversal, then T2 is a sub-string of T1 We can check this using a suffix tree However, we may hit memory limitations because suffix trees are extremely memory intensive If this become an issue, we can use an alternative approach Alternative Approach: The treeMatch procedure visits each node in the small tree at most once and is called no more than once per node of the large tree Worst case runtime is at most O(n * m), where n and m are the sizes of trees T1 and T2, respectively If k is the number of occurrences of T2’s root in T1, the worst case runtime can be characterized as O(n + k * m)

【参考代码】

 1 boolean containsTree(Node t1, Node t2)
2 {
3 if (t2 == null)
4 return true;
5 else
6 return subTree(t1, t2);
7 }
8
9 boolean subTree(Node r1, Node r2)
10 {
11 if (r1 == null)
12 return false;
13 if (r1.value == r2.value)
14 {
15 if (matchTree(r1, r2))
16 return true;
17 }
18 return (subTree(r1.left,r2) || subTree(r1.right,r2));
19 }
20
21 boolean matchTree(Node r1, Node r2)
22 {
23 if (r2 == null && r1 == null)
24 return true;
25 if (r1 == null || r2 == null)
26 return false;
27 if (r1.value != r2.value)
28 return false;
29 return (matchTree(r1.left, r2.left) && matchTree(r1.right, r2.right));
30 }

【IT笔试面试题整理】判断一个树是否是另一个的子树的更多相关文章

  1. Java笔试面试题整理第八波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51388516 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  2. Java笔试面试题整理第六波(修正版)

    转载至:http://blog.csdn.net/shakespeare001/article/details/51330745 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  3. Java笔试面试题整理第三波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51247785 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  4. Java笔试面试题整理第二波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51200163 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  5. Java笔试面试题整理第五波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51321498 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  6. Java笔试面试题整理第四波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51274685 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  7. Java笔试面试题整理第一波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51151650 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  8. C++笔试面试题整理

    朋友给出的一些常见的C++面试题,特整理如下,后期遇到新的再更新. 面试题 列举并解释C++中的四种运算符转化,说明它们的不同点: static_cast: 在功能上基本上与C风格的类型转换一样强大, ...

  9. 【IT笔试面试题整理】二叉搜索树转换为双向链表

    [试题描述] 将二叉搜索树转换为双向链表 对于二叉搜索树,可以将其转换为双向链表,其中,节点的左子树指针在链表中指向前一个节点,右子树指针在链表中指向后一个节点. 思路一: 采用递归思想,对于二叉搜索 ...

随机推荐

  1. Codeforces Round #264 (Div. 2) C. Gargari and Bishops 主教攻击

    http://codeforces.com/contest/463/problem/C 在一个n∗n的国际象棋的棋盘上放两个主教,要求不能有位置同时被两个主教攻击到,然后被一个主教攻击到的位置上获得得 ...

  2. bootstrap1.2

      <html>   <head>   <meta charset="UTF-8">   <title></title> ...

  3. delphi怎么做桌面滚动文字?

    就是在桌面显示从TXT读取出来的字,并限制在1个框内移动(就是从框左边出现往右边移动并从框边消失)我用HDC+textout只是读取字显示到桌面,不知道桌面移动哪位大侠指点下啊,或用其他方法,最好有详 ...

  4. WPF概述(硬件加速及分辨率无关性)

    一.名词解释 WPF(Windows Presentation Foundation),直译为Windows表示基础,是专门用来编写程序表示层的技术和工具. 大部分程序都是多层架构的,一般至少包含三层 ...

  5. Redis常见使用说明

    1 概述Remote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用ANSI C语言编写. ...

  6. winform自动升级方案

    未涉及过winform升级,研究一阵,大致出来个不成熟的方案. 我的解决方案(判断升级,升级程序下载安装包的压缩包,解压,自动安装,重新启动程序). 1.首先根据服务器中软件版本号和本地软件版本号是否 ...

  7. SpringBoot2 使用Spring Session集群

    有几种办法: 1.扩展指定server利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/Jetty等Serv ...

  8. lua编程之lua与C相互调用

    lua是扩展性非常良好的语言,虽然核心非常精简,但是用户可以依靠lua库来实现大部分工作.除此之外,lua还可以通过与C函数相互调用来扩展程序功能.在C中嵌入lua脚本既可以让用户在不重新编译代码的情 ...

  9. 友链&&日记

    上面友链,下面日记 友人链 最喜欢galgameの加藤聚聚 初三一本&&\(ACG\)姿势比我还丰厚的yx巨巨 更喜欢galgame的shadowice czx ZigZag胖胖 文文 ...

  10. lua 源码阅读顺序

    https://www.reddit.com/comments/63hth/ask_reddit_which_oss_codebases_out_there_are_so/c02pxbp Online ...