Subtree
2015-08-05 11:15
645 查看
You have two every large binary trees:
of nodes, and
to decide if
Have you met this question in a real interview?
Yes
Example
T2 is a subtree of T1 in the following case:
T2 isn't a subtree of T1 in the following case:
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.
题目实际上是sameTree的变形,树T2如果是另外树T1的子树,那么在T1中一定可以找到一颗子树与T2一样,那就是说,我们可以递归的检查,如果T1->val == T2->val,则应该检查isSameTree(T1, T2);如果不相等,则还应该继续递归检查isSubTree(T1->left, T2) || is SubTree(T1->right, T2)。应该属于常规的树的问题,注意各个细节的考虑。/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param T1, T2: The roots of binary tree.
* @return: True if T2 is a subtree of T1, or false.
*/
bool 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);
}
bool isSubtree(TreeNode *T1, TreeNode *T2) {
// write your code here
if(T2 == NULL)
return true;
if(T1 == NULL)
return false;
if(T1->val == T2->val && isSameTree(T1, T2))
return true;
return isSubtree(T1->left, T2) || isSubtree(T1->right, T2);
}
};
T1, with millions
of nodes, and
T2, with hundreds of nodes. Create an algorithm
to decide if
T2is 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.
题目实际上是sameTree的变形,树T2如果是另外树T1的子树,那么在T1中一定可以找到一颗子树与T2一样,那就是说,我们可以递归的检查,如果T1->val == T2->val,则应该检查isSameTree(T1, T2);如果不相等,则还应该继续递归检查isSubTree(T1->left, T2) || is SubTree(T1->right, T2)。应该属于常规的树的问题,注意各个细节的考虑。/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param T1, T2: The roots of binary tree.
* @return: True if T2 is a subtree of T1, or false.
*/
bool 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);
}
bool isSubtree(TreeNode *T1, TreeNode *T2) {
// write your code here
if(T2 == NULL)
return true;
if(T1 == NULL)
return false;
if(T1->val == T2->val && isSameTree(T1, T2))
return true;
return isSubtree(T1->left, T2) || isSubtree(T1->right, T2);
}
};
相关文章推荐
- Lintcode 比较字符串
- Lintcode 主元素
- Lintcode解题笔记 - 岛屿的个数
- 20150708 lintcode 总结 Binary Tree Maximum Path Sum Show result *****
- 20150708 lintcode 总结 Minimum Path Sum
- 20150708 lintcode 总结 Binary Search Tree Iterator
- 20150707 lintcode 总结 Search Range in Binary Search Tree
- 20150624 lintcode 总结 binary-tree-level-order-traversal **
- 20150624 lintcode 总结 Lowest Common Ancestor
- 20150624 lintcode 总结 Balanced Binary Tree
- Lintcode(1)-两个字符串是变位词
- Lintcode(2)-比较字符串
- Lintcode(3)-字符串查找
- Lintcode(4)-乱序字符串
- Lintcode(5)-最长公共子串
- Lintcode(6)-最长公共前缀
- Lintcode(7)-删除元素
- #LintCode# Binary Search
- Nth to Last Node in List
- Partition List