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

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. web-day9

    第9章WEB09-Servlet篇 今日任务 完成系统的登录的功能 完成登录系统后页面定时跳转 记录系统登录成功后,系统被访问多少次 教学导航 教学目标 了解HTTP协议 掌握Servlet的编写 了 ...

  2. java锁类型

    转载链接在每个锁类型后边 线程锁类型 1.自旋锁 ,自旋,jvm默认是10次吧,有jvm自己控制.for去争取锁 锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchroni ...

  3. java中逗号分隔的字符串和List相互转换

    1.将逗号分隔的字符串转换为List String str = "a,b,c"; List<String> result = Arrays.asList(str.spl ...

  4. poj 2192 Zipper

    题目 刚开始本来觉得可以用队列来写,但是 例如 ta te teta,ta的t先出队列那就不行了,所以还得用dp dp[i][j] 表示A前i个字符与B前j个字符是否能构成C前i+j个字符 要使 dp ...

  5. Eclipse tomcat配置 未在Eclipse中添加.jar包出错

    JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bui ...

  6. codeforces 480D

    题意:给定一些物品放置在承重为S的桌子上, 每个物品有5个属性,放置时间in,拿开时间out,重量w,承重s及放置后的收益v.而且后面放置上去的必须先拿开..求一种合法的放置使得收益最大,输出收益. ...

  7. whu暑期集训#1

    题号:SGU123----SGU131 Problem A: 题意:求斐波那契的前N项和.. 做法:直接模拟,注意得用long long Problem B: 题意:给定一个封闭的多边形,求一个点在不 ...

  8. 关于DFS和BFS的理解 以及坐标的定义

    http://blog.csdn.net/bool_isprime/article/details/5803018DFS: 1: 坐标类型搜索 :这种类型的搜索题目通常来说简单的比较简单,复杂的通常在 ...

  9. string转Date转回String(JAVA)

    String dateString = "2012-12-06 ";   SimpleDateFormat sdf = new SimpleDateFormat("yyy ...

  10. [leed code 179] Largest Number

    1 题目 Given a list of non negative integers, arrange them such that they form the largest number. For ...