【IT笔试面试题整理】判断一个树是否是另一个的子树
【试题描述】定义一个函数,输入判断一个树是否是另一个对的子树
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笔试面试题整理】判断一个树是否是另一个的子树的更多相关文章
- Java笔试面试题整理第八波
转载至:http://blog.csdn.net/shakespeare001/article/details/51388516 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第六波(修正版)
转载至:http://blog.csdn.net/shakespeare001/article/details/51330745 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第三波
转载至:http://blog.csdn.net/shakespeare001/article/details/51247785 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第二波
转载至:http://blog.csdn.net/shakespeare001/article/details/51200163 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第五波
转载至:http://blog.csdn.net/shakespeare001/article/details/51321498 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第四波
转载至:http://blog.csdn.net/shakespeare001/article/details/51274685 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第一波
转载至:http://blog.csdn.net/shakespeare001/article/details/51151650 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- C++笔试面试题整理
朋友给出的一些常见的C++面试题,特整理如下,后期遇到新的再更新. 面试题 列举并解释C++中的四种运算符转化,说明它们的不同点: static_cast: 在功能上基本上与C风格的类型转换一样强大, ...
- 【IT笔试面试题整理】二叉搜索树转换为双向链表
[试题描述] 将二叉搜索树转换为双向链表 对于二叉搜索树,可以将其转换为双向链表,其中,节点的左子树指针在链表中指向前一个节点,右子树指针在链表中指向后一个节点. 思路一: 采用递归思想,对于二叉搜索 ...
随机推荐
- Android webview 开启地理位置定位
WebSettings webSettings = webView.getSettings(); webSettings.setDatabaseEnabled(true); String dir = ...
- elasticsearch5环境搭建
1.下载ElasticSearch https://www.elastic.co/cn/downloads/elasticsearch#ga-release 因为是windows版本,所以下载zip即 ...
- [ 9.10 ]CF每日一题系列—— 186A模拟处理字符串
Description: 跟你两个不相同的字符串,问你能否将第一个字符串任意两个字母交换一次使得两字符串相同,YES or NO Solution: 一维模拟就好了 #include <iost ...
- 软件测试实践平台(Mooctest)FAQ
0. 背景 我们在<软件测试技术>课程使用 MOOCTEST (mooctest.net) 作为课程的实践教学平台. 在教学过程中学生们遇到了一些问题,本文将一一总结. 注意:本文在解决问 ...
- ASP.Net MVC OA项目笔记<六>
1.1.1 开始写业务,先写业务的实现再写业务的接口 业务类中也是有写增删改查公用的方法 引用Model,IDAL,DALFactory BLL添加两个类 UserInfoService,BaseSe ...
- ASP.NET MVC一次删除多笔记录 V2.0
前一段时间Insus.NET有写一篇<ASP.NET MVC一次删除多笔记录>http://www.cnblogs.com/insus/p/6241186.html 可以前往去看看. 觉得 ...
- eclipse maven打war包
在eclipse中找到pom.xml文件右键 选择debug as 再选择Maven install运行后 按路径找到生成的war包 推荐https://www.cnblogs.com/qlqwjy/ ...
- java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)
// java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...
- [LeetCode]138复制带随机指针的链表
题目描述: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深度拷贝. 思路: 先遍历链表,将每个节点对应的随机指针指向的对象利用Hash ...
- 【xsy2272】 与运算 状压dp
题目大意:给你一个长度为$n$的序列$a$,我们定义$f_i$表示序列$a$前i项一次进行按位与运算后的值. 我们认为一个序列的价值为$\sum_{i=1}^{n}f_i$,现在你要重新排列序列$a$ ...