Determine if a binary tree is a subtree of another binary tree

Source: Internet
Author: User

Points: two fork tree understanding, recursive application


Test instructions: such as binary tree A, as follows:

16

7 28

                6                 &NBSP ;                    14               & nbsp                            23       & nbsp                          ,         &NB Sp    

      3                           &NBSP ;          /                &NBS       P                          ,         &NB Sp  27                      30                      55

8 29

Judge the other two forks of Trees B, C, D, E, F, G in turn as follows:

                                                                   1 4                                 &NBSP ;                                                              28

                              &NBS   P                        12         &N Bsp            12      15                       +                                  23

                           8                                ,         &N Bsp                           9         & nbsp                    29               & nbsp                          ,         &NB Sp        27      

27

The sword refers to the offer surface question 18


Idea: This is a typical problem, first of all must be clear what kind of two fork tree can be called another binary tree sub-binary tree, to see the conclusion:
B:yes

C:no, Missing leaf node 8

D:no, leaf node 9 and original binary tree leaf node 8 inconsistent

E:no, more leaf nodes

f:yes,30 subtree only 30 itself and leaf node 29

The subtree of g:no,28 and the right subtree of 41 nodes.

That is, it must be the complete subtree, from the subtree to the leaf, whether the shape, the value, the number must be exactly the same


So, determine if the steps are subtree:

1, the original binary tree to go forward sequence traversal, trying to find out which node value and the root node of the tree is judged the same;

If I didn't find it until the end, it wouldn't be.

2, if found, the two binary tree together with the pre-sequence traversal, to try to find two two fork tree at the same time, and the same number of nodes in the same left and right sub-tree traversal process of the same value, while the completion of the traversal of the same shape, the nodes in the process of the left and right sub-tree values are the same You can think of it as a subtree.


Code:

#include <iostream> template<class t> struct Node {T val;
    Node<t> *lchild;
    Node<t> *rchild;

Node (int _val): Val (_val), Lchild (nullptr), Rchild (nullptr) {}};

Template<class t> class Btree {node<t> *root; Public:btree (): root (nullptr) {} void Free (node<t> *cur) {if (cur) {free (Cur->lchil
            D);
            Free (cur->rchild);
            Delete cur;
        cur = nullptr;
    }} ~btree () {free (root);
    } node<t> *getroot () {return root;
        } void Add (int val) {if (!root) {root = new node<t> (val);
            } else {node<t> *cur = root;
                        while (cur) {if (Cur->val > Val) {if (cur->lchild) {
                    Cur = cur->lchild;
  } else {node<t> *newnode = new Node<t> (val);                      Cur->lchild = NewNode;  }} else if (Cur->val < val) {if (cur->rchild) {cur
                    = cur->rchild;
                        } else {node<t> *newnode = new Node<t> (val);
                    Cur->rchild = NewNode;
                }} else {break; }}}} bool Judge (node<t> *cur1, node<t> *cur2) {if (!cur1 &&
        !CUR2) {return true; } if (Cur1 && cur2) {if (Cur1->val = = cur2->val) {bool res_l
                = Judge (Cur1->lchild, cur2->lchild);
                BOOL Res_r = Judge (Cur1->rchild, cur2->rchild);
            return res_l & Res_r;
                } else {bool res_l = Judge (Cur1->lchild, CUR2); if (res_l) {
                    return res_l;
                } else if (!res_l) {return Judge (Cur1->rchild, CUR2);
        }}} else {return false; }} bool Judge_whether_is_a_subtree (Btree &obt) {Judge (root, obt.
    Getroot ());

}
};
    int main () {btree<int> bt1, BT2, BT3, Bt4, Bt5, Bt6, Bt7, bt8; Bt1.
    ADD (16); Bt1.
    ADD (7); Bt1.
    ADD (28); Bt1.
    ADD (6); Bt1.
    ADD (14); Bt1.
    ADD (23); Bt1.
    ADD (41); Bt1.
    ADD (3); Bt1.
    ADD (12); Bt1.
    ADD (15); Bt1.
    ADD (27); Bt1.
    ADD (30); Bt1.
    ADD (55); Bt1.
    ADD (8); Bt1.

    ADD (29); Bt2.
    ADD (14); Bt2.
    ADD (12); Bt2.
    ADD (15); Bt2.

    ADD (8); BT3.
    ADD (14); BT3.
    ADD (12); BT3.

    ADD (15); Bt4.
    ADD (14); Bt4.
    ADD (12); Bt4.
    ADD (15); Bt4.

    ADD (9); Bt5.
    ADD (41); Bt5.
    ADD (30); Bt5.
    ADD (55); Bt5.
    ADD (29); Bt5.
    
    ADD (37); Bt6.
    ADD (30); Bt6.

    ADD (29); Bt7.
    ADD (28); Bt7.
ADD (23);    Bt7.
    
    ADD (27); Bt8.
    
    ADD (129); Std::cout << bt1.
    Judge_whether_is_a_subtree (BT2) << Std::endl; Std::cout << bt1.
    Judge_whether_is_a_subtree (BT3) << Std::endl; Std::cout << bt1.
    Judge_whether_is_a_subtree (BT4) << Std::endl; Std::cout << bt1.
    Judge_whether_is_a_subtree (BT5) << Std::endl; Std::cout << bt1.
    Judge_whether_is_a_subtree (BT6) << Std::endl; Std::cout << bt1.
    Judge_whether_is_a_subtree (BT7) << Std::endl; Std::cout << bt1.
    Judge_whether_is_a_subtree (BT8) << Std::endl;
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.