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

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. Android面试题整理(selfmade)——坚持每天回答一个

    这部分面试题基本都是在2011年常见的. 发几个常见.大部分直接copy 其实好多你只要理解大致的意思就行了.解答都是偏长. 1.如何避免ANR? 答:ANR:Application Not Resp ...

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

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

  3. 网易历届笔试面试题整理大全

    整理了一下网易往届笔试面试题,希望对大家有帮助: 超级有用的面试题:Java常见面试题    常见算法面试题   数据库常见面试题  操作系统常见面试题   C/C++常见面试题  大数据常见面试   ...

  4. 【IT笔试面试题整理】堆栈和队列

    如何准备: Whether you are asked to implement a simple stack / queue, or you are asked to implementa modi ...

  5. 【IT笔试面试题整理】链表

    如何准备 Linked list questions are extremely common These can range from simple (delete a node ina linke ...

  6. 【IT笔试面试题整理】判断链表是否存在环路,并找出回路起点

    [试题描述]定义一个函数,输入一个链表,判断链表是否存在环路,并找出回路起点 Circular linked list: A (corrupt) linked list in which a node ...

  7. 【IT笔试面试题整理】给定一个数组a[N]构造数组b [N]

    [来源]:腾讯2013实习生笔试   给定一个数组a[N],我们希望构造数组b [N],其中b[j]=a[0]*a[1]-a[N-1] / a[j],在构造过程中,不允许使用除法:要求O(1)空间复杂 ...

  8. 【IT笔试面试题整理】给定二叉树,给每层生成一个链表

    [试题描述]定义一个函数,给定二叉树,给每层生成一个链表 We can do a simple level by level traversal of the tree, with a slight ...

  9. 【IT笔试面试题整理】字符串的排列

    [试题描述]输入一个字符串,打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab,cba. 分析:这是一道很好的考查对 ...

最新文章

  1. 点云标注工具:1.PCAT
  2. 芯片的未来,靠这些技术了
  3. ylbtech-LanguageSamples-Generics(泛型)
  4. 查询页面代码运行时间
  5. 【鸿蒙 HarmonyOS】UI 组件 ( 单选按钮 | RadioButton 与 RadioContainer 组件 )
  6. wordpress文章添加css样式,给WordPress文章循环加上CSS类方便实现各种页面布局
  7. 台式机没有显示计算机图标,为什么台式电脑没有喇叭图标
  8. Unity项目文件夹结构
  9. Atitit. 构造ast 语法树的总结attilax oao
  10. PROTEL网络教程前
  11. java线程生命周期的图示以及文字说明
  12. 深度linux deepin15.2,从其它Deepin版本升级到深度Deepin 15.11操作系统的方法
  13. 智慧城市解决方案(智慧城市系统及相关技术)
  14. DJFocus是个什么东东
  15. 关于华为应用市场的上架流程
  16. java 数字补零_java数字位数不足在前后补0
  17. 51nod 1830
  18. css 文字两端对齐
  19. 安卓版微信 input onchange事件不生效
  20. 你爱的那个她,跨年祝福送到了吗?元旦倒计时,雄起~

热门文章

  1. vs dll lib 使用记录
  2. tkinter 类继承的三种方式
  3. 加密数据包--加解密部分逆向跟踪
  4. 思想:CoreMVC是什么(3)
  5. 部署Small Business Server 2003服务器之一
  6. CMMI3组织级文档列表清单
  7. c语言判定三角形方法,c语言判定三角形的各种类型——请大家指点
  8. 微博取关列表怎么看_微表情心理学:教你怎么从手的动作,去看他人真实的内心想法...
  9. java第九章实验报告_2019JAVA第九次实验报告
  10. Yet Another Counting Problem CodeForces - 1342C(规律+前缀和)