两棵树的最大公共子树_两棵镜树

太过爱你忘了你带给我的痛 2023-03-05 09:37 106阅读 0赞

两棵树的最大公共子树

Problem statement: Given a Two Binary Trees, write a function that returns true if one is mirror of other, else returns false.

问题陈述:给定两个二叉树,编写一个函数,如果一个是另一个的镜像,则返回true,否则返回false。

Solution

Check the below examples:

检查以下示例:

Mirror Tress

Not Mirror Tress

In figure 1, both the trees have the same root and one tree is mirror of other. Where as in In Figure 2, both trees are identical in shape and thus are not mirror trees at all and have different node values too.

在图1中,两棵树具有相同的根,一棵树是另一棵的镜像。 如图2所示,两棵树的形状相同,因此根本不是镜像树,并且节点值也不同。

So in general, two trees are said to be mirror trees of each other if they have the same root , left subtree (from root) of first tree is mirror of right subtree of second tree & right subtree of the first tree is mirror of left subtree of second tree.

因此,通常,如果两棵树具有相同的根,则称它们为彼此的镜像树,第一棵树的左子树(从根开始)是第二棵树的右子树的镜像,而第一棵树的右子树是左树的镜像第二棵树的子树

These are the three necessary & sufficient conditions to be checked for finding mirror trees.

这是查找镜像树需要检查的三个必要条件。

Algorithm:

算法:

  1. 1. Define tree structure like:
  2. class tree{ // tree node is defined
  3. public:
  4. int data;
  5. tree *left;
  6. tree *right;
  7. };
  8. Or you can use struct instead of class
  9. 2. Build both of the input tree.
  10. 3. Recursive function AreMirror to check whether
  11. both trees are mirror tree or not.
  12. FUNCTION AreMirror (root of first tree, root of second tree)
  13. Root of first tree, root1
  14. Root of second tree root2
  15. FUNCTION AreMirror (root1, root2)
  16. a. Check base cases
  17. If root1==NULL&&root2==NULL
  18. Then its mirror tree, return true;
  19. If root1==NULL || root2==NULL
  20. Then its not a mirror tree, return false
  21. Because one root is NULL whether another is not.
  22. (Both cant be NULL here, since already checked before)
  23. If root1->data != root2->data
  24. Then its not a mirror tree, return false.
  25. Because roots are different & thus cant be mirror image of other
  26. b. Recursively check for sub-trees
  27. return(AreMirror(root1->left, root2->right)
  28. &&AreMirror(root1->right,root2->left));
  29. END FUNCTION

Example & Explanation:

示例与说明:

Let’s check how the program works for the first example…

让我们检查第一个示例的程序工作方式…

N.B: Nodes are represented by their respective values.

注意:节点由它们各自的值表示。

  1. Root1=1 and Root2=1
  2. In the main it calls AreMirror (1, 1)
  3. --------------------------------------------------
  4. AreMirror (1, 1):
  5. 1->left =2 and 1->right=3 in case of tree1
  6. 1->left =3 and 1->right=2 in case of tree2
  7. No base cases are satisfied thus it returns,
  8. (AreMirror ( 2, 2) && AreMirror ( 3, 3))
  9. Call to AreMirror ( 2, 2) and AreMirror ( 3, 3)
  10. --------------------------------------------------
  11. AreMirror (2, 2):(call at AreMirror (1, 1))
  12. 2->left =4 and 2->right=NULL in case of tree1
  13. 2->left =NULL and 2->right=4 in case of tree2
  14. No base cases are satisfied thus it returns,
  15. (AreMirror (4, 4) && AreMirror (NULL, NULL))
  16. Call to AreMirror (4, 4) and AreMirror (NULL, NULL))
  17. --------------------------------------------------
  18. AreMirror (3, 3): (call at AreMirror (1, 1))
  19. 3->left =5 and 3->right=NULL in case of tree1
  20. 3->left =NULL and 3->right=5 in case of tree2
  21. No base cases are satisfied thus it returns,
  22. (AreMirror (5, 5) && AreMirror (NULL, NULL))
  23. Call to AreMirror (5, 5) && AreMirror (NULL, NULL)
  24. --------------------------------------------------
  25. AreMirror (4, 4): (call at AreMirror (2, 2))
  26. 4->left =NULL and 4->right=NULL in case of tree1
  27. 4->left =NULL and 4->right=NULL in case of tree2
  28. No base cases are satisfied thus it returns,
  29. (AreMirror (NULL, NULL) && AreMirror (NULL, NULL))
  30. Call to AreMirror (NULL, NULL) && AreMirror (NULL, NULL)
  31. --------------------------------------------------
  32. AreMirror (NULL, NULL): (call at AreMirror (2, 2))
  33. Base case matches and returns 1.
  34. --------------------------------------------------
  35. AreMirror (5, 5): (call at AreMirror (3, 3))
  36. 5->left =NULL and 5->right=NULL in case of tree1
  37. 5->left =NULL and 5->right=NULL in case of tree2
  38. No base cases are satisfied thus it returns,
  39. (AreMirror (NULL, NULL) && AreMirror (NULL, NULL))
  40. Call to AreMirror (NULL, NULL) && AreMirror (NULL, NULL)
  41. --------------------------------------------------
  42. AreMirror (NULL, NULL): (call at AreMirror (3, 3))
  43. Base case matches and returns 1.
  44. --------------------------------------------------
  45. AreMirror (NULL, NULL): (call at AreMirror (4, 4))
  46. Base case matches and returns 1.
  47. --------------------------------------------------
  48. AreMirror (NULL, NULL): (call at AreMirror (4, 4))
  49. Base case matches and returns 1.
  50. --------------------------------------------------
  51. AreMirror (NULL, NULL): (call at AreMirror (5, 5))
  52. Base case matches and returns 1.
  53. --------------------------------------------------
  54. AreMirror (NULL, NULL): (call at AreMirror (5, 5))
  55. Base case matches and returns 1.
  56. --------------------------------------------------
  57. Thus at main,
  58. AreMirror (1, 1) returns:
  59. = AreMirror ( 2, 2) && AreMirror ( 3, 3)
  60. = (AreMirror (4, 4) && AreMirror (NULL, NULL)) &&
  61. (AreMirror (5, 5) && AreMirror (NULL, NULL))
  62. = ((AreMirror (NULL, NULL) && AreMirror (NULL, NULL)) && 1)
  63. &&((AreMirror (NULL, NULL) && AreMirror (NULL, NULL)) && 1)
  64. = ((1 && 1) &&1) && (1 && 1) &&1)
  65. = 1
  66. Thus they are mirror trees

用C ++实现检查两棵树是否是镜像? (C++ implementation to check whether two trees are mirros?)

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class tree{
  4. // tree node is defined
  5. public:
  6. int data;
  7. tree *left;
  8. tree *right;
  9. };
  10. tree* newnode(int data) // creating new node
  11. {
  12. tree* node = (tree*)malloc(sizeof(tree));
  13. node->data = data;
  14. node->left = NULL;
  15. node->right = NULL;
  16. return(node);
  17. }
  18. //function to check mirror tree
  19. int areMirror(tree* a, tree* b)
  20. {
  21. // base cases
  22. //if both root is NULL, then it's mirror tree
  23. if(a==NULL && b==NULL)
  24. return 1;
  25. //if one is NULL & other is not
  26. //then it's not mirror tree
  27. if(a==NULL || b==NULL)
  28. return 0;
  29. //if root data are different
  30. //not mirror tree
  31. if(a->data!=b->data)
  32. return 0;
  33. //check for subtrees recursively
  34. return(areMirror(a->left,b->right) && areMirror(a->right,b->left));
  35. }
  36. int main(){
  37. //**same tree is builted as shown in example 1**
  38. cout<<"**same trees are built as shown in example 1**\n";
  39. tree *root1=newnode(1);
  40. root1->left= newnode(2);
  41. root1->right= newnode(3);
  42. root1->left->left=newnode(4);
  43. root1->right->left=newnode(5);
  44. tree *root2=newnode(1);
  45. root2->left= newnode(3);
  46. root2->right= newnode(2);
  47. root2->right->right=newnode(4);
  48. root2->left->right=newnode(5);
  49. cout<<"printing whether mirror trees...\n";
  50. if(areMirror(root1,root2))//if returns 1
  51. cout<<"Both are mirror of each other\n";
  52. else
  53. cout<<"not mirror of each other\n";
  54. //**same tree is builted as shown in example 2**
  55. cout<<"**same trees are built as shown in example 2**\n";
  56. root1=newnode(2);
  57. root1->left= newnode(7);
  58. root1->right= newnode(5);
  59. root1->right->right=newnode(9);
  60. root1->right->right->left=newnode(4);
  61. root1->left->left=newnode(2);
  62. root1->left->right=newnode(6);
  63. root1->left->right->left=newnode(5);
  64. root1->left->right->right=newnode(11);
  65. root2=newnode(8);
  66. root2->left= newnode(3);
  67. root2->right= newnode(10);
  68. root2->right->right=newnode(14);
  69. root2->right->right->left=newnode(13);
  70. root2->left->left=newnode(1);
  71. root2->left->right=newnode(6);
  72. root2->left->right->left=newnode(4);
  73. root2->left->right->right=newnode(7);
  74. cout<<"printing whether mirror trees...\n";
  75. if(areMirror(root1,root2))//if returns 1
  76. cout<<"Both are mirror of each other\n";
  77. else
  78. cout<<"not mirror of each other\n";
  79. return 0;
  80. }

Output

输出量

  1. **same trees are built as shown in example 1**
  2. printing whether mirror trees...
  3. Both are mirror of each other
  4. **same trees are built as shown in example 2**
  5. printing whether mirror trees...
  6. not mirror of each other

翻译自: https://www.includehelp.com/icp/two-mirror-trees.aspx

两棵树的最大公共子树

发表评论

表情:
评论列表 (有 0 条评论,106人围观)

还没有评论,来说两句吧...

相关阅读