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

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. Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. poj 1753 2965

    这两道题类似,前者翻转上下左右相邻的棋子,使得棋子同为黑或者同为白.后者翻转同行同列的所有开关,使得开关全被打开. poj 1753 题意:有一4x4棋盘,上面有16枚双面棋子(一面为黑,一面为白), ...

  3. Stein算法求最大公约数

    首先引进一个符号:gcd是greatest common divisor(最大公约数)的缩写,gcd( x,y ) 表示x和y的最大公约数.然后有一个事实需要了解:一个奇数的所有约数都是奇数.这个很容 ...

  4. int LL数据范围

    unsigned int 0-4294967295 0~4e9 int -2147483648-2147483647 -2e9~2e9 unsigned long 0-4294967295 long ...

  5. iOS 使用代码创建约束,实现自动布局

    ///与下面约束对象属性截图相对应//使用Auto Layout约束,禁止将Autoresizing Mask转换为约束 [self.funcView setTranslatesAutoresizin ...

  6. INDEX--从数据存放的角度看索引

    测试表结构: CREATE TABLE TB1 ( ID ,), C1 INT, C2 INT ) 1. 聚集索引(Clustered index) 聚集索引可以理解为一个包含表中除索引键外多有剩余列 ...

  7. Download SQL Server Management Studio (SSMS)下载地址

    Download SQL Server Management Studio (SSMS)下载地址: https://msdn.microsoft.com/en-us/library/mt238290. ...

  8. 关于ubuntu软件卸载的问题

    ...... 起因很菜....就是手贱把/usr/lib/下的R的目录给rm -rf掉了, 然后在R的软件包里用./configure也生不成, R RHOME还在, 然而因为lib下的R删掉了所以R ...

  9. pyqt5 Button.click 报错:argument 1 has unexpected type 'NoneType'

    如上所示,在括号中,添加‘lambda:’,就可以成功运行,不知道为啥. 参考:https://blog.csdn.net/flhsxyz/article/details/79220936?utm_s ...

  10. Python基础 --函数的参数

    定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑被封装起来,调用者无需了解 ...