您的位置:首页 > 其它

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: