Lintcode: Subtree
2016-02-08 23:45
302 查看
You have two every large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1. Have you met this question in a real interview? Yes Example T2 is a subtree of T1 in the following case: 1 3 / \ / T1 = 2 3 T2 = 4 / 4 T2 isn't a subtree of T1 in the following case: 1 3 / \ \ T1 = 2 3 T2 = 4 / 4 Note A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of n is identical to T2. That is, if you cut off the tree at node n, the two trees would be identical.
方法1: Traverse the tree T in preorder fashion. For every visited node in the traversal, see if the subtree rooted with this node is identical to S.
Time Complexity: Time worst case complexity of above solution is O(mn) where m and n are number of nodes in given two trees.
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param T1, T2: The roots of binary tree. * @return: True if T2 is a subtree of T1, or false. */ public boolean isSubtree(TreeNode T1, TreeNode T2) { // write your code here if (T2 == null) return true; if (T1 == null) return false; if (isSameTree(T1, T2)) return true; return isSubtree(T1.left, T2) || isSubtree(T1.right, T2); } public boolean isSameTree(TreeNode T1, TreeNode T2) { if (T1==null && T2==null) return true; if (T1==null || T2==null) return false; if (T1.val != T2.val) return false; return isSameTree(T1.left, T2.left) && isSameTree(T1.right, T2.right); } }
方法2:
In this post a O(n) solution is discussed. The idea is based on the fact that inorder and preorder/postorder uniquely identify a binary tree. Tree S is a subtree of T if both inorder and preorder traversals of S arew substrings of inorder and preorder traversals of T respectively.
Following are detailed steps.
1) Find inorder and preorder traversals of T, store them in two auxiliary arrays inT[] and preT[].
2) Find inorder and preorder traversals of S, store them in two auxiliary arrays inS[] and preS[].
3) If inS[] is a subarray of inT[] and preS[] is a subarray preT[], then S is a subtree of T. Else not.
We can also use postorder traversal in place of preorder in the above algorithm.
Time Complexity: Inorder and Preorder traversals of Binary Tree take O(n) time. The function strstr() can also be implemented in O(n) time using KMP string matching algorithm.
Auxiliary Space: O(n)
可惜没有过Lintcode big case
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param T1, T2: The roots of binary tree. * @return: True if T2 is a subtree of T1, or false. */ public boolean isSubtree(TreeNode T1, TreeNode T2) { // write your code here if (T2 == null) return true; if (T1 == null) return false; StringBuffer in1 = new StringBuffer(); StringBuffer in2 = new StringBuffer(); inorder(in1, T1); inorder(in2, T2); StringBuffer pre1 = new StringBuffer(); StringBuffer pre2 = new StringBuffer(); preorder(pre1, T1); preorder(pre2, T2); return isSubstring(in2.toString(), in1.toString()) && isSubstring(pre2.toString(), pre1.toString()); } public void inorder(StringBuffer sb, TreeNode cur) { if (cur == null) return; inorder(sb, cur.left); sb.append(cur.val); inorder(sb, cur.right); } public void preorder(StringBuffer sb, TreeNode cur) { if (cur == null) return; sb.append(cur.val); preorder(sb, cur.left); preorder(sb, cur.right); } public boolean isSubstring(String str2, String str1) { for (int i=0; i<=str1.length()-str2.length(); i++) { String sub = str1.substring(i, i+str2.length()); if (sub.equals(str2)) return true; } return false; } }
相关文章推荐
- 学习markdown心得
- SpeedTree - 导入模型到UE4
- UTC、GTC时间和本地时间
- 十六进制转十进制
- go语言入门的资料
- poj 3368 Frequent values(RMQ)
- BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )
- 生命还是要适可而止为好 《转》
- 黑马程序员-----java基础01第一个应用程序 "HelloWorld"
- 支付宝春晚互动:79万人集齐五福平分2.15亿红包
- 支付宝春晚互动:79万人集齐五福平分2.15亿红包
- Android开发笔记(五十八)铃声与震动
- A - 高精度(大数)N次方(第二季水)
- 积跬步至千里系列之七--应用程序的安装与卸载(二)
- HDU-1133Buy the Ticket 卡特兰数买票问题
- STC15系列4个串口的使用库函数
- 【JAVA】3、常用代码注释
- 2.4 Android SDK在线更新镜像服务器,Androidsdk 在线更新
- poj3692
- Android 混淆代码总结