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

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. common.php

    <?php /** * */ class Common { if(!function_exists('is_php')) { function is_php($version = '5.0.0' ...

  2. 微软新一代Surface,该怎么看?

    近日,微软在美国纽约发布了其全新一代产品——Surface 2和Surface Pro 2.如果留意微软官方商城的话,可以看到该产品现已全面开放预购.那么,这样一款产品到底怎么样?让我们来一个横向的对 ...

  3. CRC校验3种算法_转载

    //CRC16校验在通讯中应用广泛,这里不对其理论进行讨论,只对常见的3种//实现方法进行测试.方法1选用了一种常见的查表方法,类似的还有512字//节.256字等查找表的,至于查找表的生成,这里也略 ...

  4. Java-动态代理技术

    1.程序中的代理 为具有相同接口的目标类的各个方法,添加一些系统功能,如日志,异常处理,计算方法运行的 时间,事务管理等等,都可以交给另一个类去实现这些功能,该类称为代理类. 注意:为了让代理类共享目 ...

  5. NFS Server宕机后,NFS Client主机上df命令挂死

    方法1: 使用root用户:Oracle@NDMCDB05:~> su -Password: NDMCDB05:~ # cat /etc/mtab /dev/sda2 / reiserfs rw ...

  6. 深度学习主机环境配置: Ubuntu16.04+GeForce GTX 1080+TensorFlow

    接上文<深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0>,我们继续来安装 TensorFlow,使其支持GeForce GTX 1080显卡 ...

  7. RouteOS 频繁自启

      本来是一个美好的大周末,突然却被一个突如其来的电话把我从美梦中惊醒,然而一切还不止这么简单......   本来刚开始了解到信息是客户的一台RouteOS设备挂了,听到这个消息时觉得自己应该可以很 ...

  8. 动态产生select option列表

    在很久之前,Insus.NET刚学习前端时,有写过<动态创建一些常的html标签>http://www.cnblogs.com/insus/p/3741665.html 但其中没有实现动态 ...

  9. 操作Checkbox标签

    在前端开发中,少不了对Checkbox的操作. 常用的的方法有2个:.is()和.prop()方法.前者是判断 checkbox的状态,选不是未选.而后者为checkbox设置一个值,可以设置chec ...

  10. 【五校联考3day2】C

    題意: 現有一平面直角坐標系,有n個點,每一個點必須向某一個方向發射射線,且任意一條射線必須與某一條坐標軸平行.定義一種發射射線的方案是合法的,則方案必須滿足: 1.沒有一條射線交叉 2.沒有一條射線 ...