Sword refers to the substructure of the offer--tree (Java code)

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Title Description :

Enter two binary trees, a, B, to determine whether a is a sub-structure of a. (PS: We agree that an empty tree is not a sub-structure of any tree).

Problem Solving Ideas:

  First look at the test cases given by the cattle network:

  

Generally for the operation of the tree is not like a linked list, the operation is more complex, if the use of circular traversal, for the non-complete binary tree rule is difficult to find, the general method is the use of recursive solution, the subject is no exception, the same use recursive solution, The general idea of the solution is to first determine whether the root node of B is the same as the root node of a (here the same is the same value of the node and the same as the left and right child nodes), if the same comparison of their left and right child nodes, this step is the same, can be done recursively, until B traversal to each tail node, If the process compares all the nodes that are the same, it proves that B is a sub-structure. If the root node of B differs from the root node of a, then a slides to the left and right child nodes, then continues to compare with the child nodes of B, as in the previous step.

The use of recursion can be done in three steps that I have summed up. The solution process is as follows:

1. Recursive cutoff conditions. Recursive cutoff conditions are for recursion to avoid infinite loops, first to analyze what happens when recursive cutoff returns the traversal results, (1) According to the topic requirements, if B number is an empty tree, recursive cutoff. (2) If the traversed tree A is an empty tree, the recursive cutoff is natural. (3) If the tail node of B is compared, it cannot proceed, and the recursion will be cut off.  (4) If a tree traverses from the beginning to the tail, there is no node of the same node as B, and the recursive cutoff.  2. Recursion and cohesion. If the root node value of a and the left and right child nodes are exactly the same as the root node of B, then both A and B continue to slide to their left and right child nodes for comparison, and if the root node value of a is the same as the root node value of B, but the condition of the left and right child nodes is not the same, then only the  If the value of the root node of a is not the same as the value of the root node of B, then a slides directly to his or her own point and then to the root node of B, until the traversal is complete.  3. Processing of recursive node data. According to the topic, the recursive merge used in this topic does not have data processing, but compares two tree nodes to determine whether the same. For other recursion, the specific situation can be treated concretely.

Source:

1     /**2 * Enter two binary trees A, B, to determine whether the sub-structure of a. 3      * @paramRoot1 a tree4      * @paramRoot2 B number5      * @return 6      */7      Public Static BooleanHassubtree (TreeNode root1,treenode root2) {8         9         if(root2==NULL) {//an empty tree is not a sub-structure of any treeTen             return false; One         } A         if(root1==NULL) {//If A is empty, it must return False -             return false; -         } the         if(Root2.val==root1.val) {//The same value as the root node of a and B comparisons -              -             if(root2.left==NULL&&root2.right==NULL) {//The node that is compared is the tail node of B, and the recursive cutoff -                 return true; +             } -             //The following three comparisons are in the exact same situation as the nodes of the comparison +             if((root2.left!=NULL&&root2.right!=NULL) && (root1.left!=NULL&&root1.right!=NULL) &&root2.left.val==root1.left.val&&root2.right.val==root1.right.val) { A                 returnHassubtree (Root1.left,root2.left) &&Hassubtree (root1.right,root2.right); at}Else if((root2.left!=NULL&&root2.right==NULL) && (root1.left!=NULL&&root1.right==NULL) &&root2.left.val==root1.left.val) { -                 returnHassubtree (root1.left,root2.left); -}Else if((root2.left==NULL&&root2.right!=NULL) && (root1.left==NULL&&root1.right!=NULL) &&root2.right.val==root1.right.val) { -                 returnHassubtree (root1.right,root2.right); -}Else{//compared to the node, a to the left and right child nodes to move a re-comparison -                 if(root1.left!=NULL&&root1.right!=NULL) { in                     returnHassubtree (root1.left,root2) | |Hassubtree (ROOT1.RIGHT,ROOT2); -}Else if(root1.left==NULL&&root1.right!=NULL){ to                     returnHassubtree (ROOT1.RIGHT,ROOT2); +}Else if(root1.left!=NULL&&root1.right==NULL){ -                     returnHassubtree (ROOT1.LEFT,ROOT2); the}Else { *                     return false; $                 }    Panax Notoginseng             } -         //The value of the root node of the comparison is different, sliding directly to the left and right child nodes the}Else if(root1.left!=NULL&&root1.right==NULL){ +             returnHassubtree (ROOT1.LEFT,ROOT2); A}Else if(root1.left==NULL&&root1.right!=NULL){ the             returnHassubtree (ROOT1.RIGHT,ROOT2); +}Else{ -             return false; $         } $}

Sword refers to the substructure of the offer--tree (Java code)

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.