Determine whether the T1 tree contains all the topological structures of the T2 tree __boolean

Source: Internet
Author: User

Determines whether the T1 tree contains all the topologies of the T2 tree public class containsubtree{the public static class node{the public int value;
   	   Public Node left;

   	   public Node right;
   	   public Node (int data) {this.value=data;
           }//To determine whether to include public static Boolean Iscontainsubtree (Node h1,node h2) {if (h2==null) {
        return true;
        } if (H1==null) {return false; ///Three case matching (recursive call) return check (H1,H2) | | Iscontainsubtree (H1.LEFT,H2) | |
    Iscontainsubtree (H1.RIGHT,H2);
    	public static Boolean check (Node h1,node h2) {if (H2==null) {return true; } if (h1==null| |
    	H1.value!=h2.value) {return false;

    return check (h1.left,h2.left) &&check (h1.right,h2.right);
       public static void Main (String []args) {//system.out.print ("Hello");
       Node H1=new node (1);
       H1.left=new Node (2); H1.right=new Node(3);
       H1.left.left=new Node (4);
       H1.left.right=new Node (5);
       H1.right.left=new Node (6);
       H1.right.right=new Node (7);
       H1.left.left.left=new Node (8);
       H1.left.left.right=new Node (9);

       H1.left.right.left=new Node (10);
       Node H2=new node (2);
       H2.left=new Node (4);
       H2.right=new Node (5);
       
       H2.left.left=new Node (8);
   System.out.print (Iscontainsubtree (H1,H2)); }
}


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.