Sword refers to the offer face question (Java version): The substructure of the tree __java

Source: Internet
Author: User

Topic: Enter two binary trees A and B to determine whether B is a substructure.

For example, the two binary trees shown in the figure, because a part of the subtree structure and B is the same, so B is a substructure.


To find if there is a subtree in tree a that is the same as the tree B structure, we can take two steps: The first step is to find the same node R in tree A as the value of the root node of B, and the second step is to determine whether the subtree with R as the root in tree a contains the same structure as tree B.

Take the two trees above as an example to analyze the process in detail. First we try to find a node with a value of 8 in tree A. Starting at the root node of tree A, we find that the value of its root node is 8. Then we'll determine if the subtree below the root node of tree A has the same structure as tree B. In tree A, the value of the Zoozi node of the root node is 8, and the Zoozi node of the root of tree B is 9, and the corresponding two nodes are different.

So we still have to go through a and look for a node with a value of 8. We found a node with a value of 8 in the second layer of the tree, and then we judged the second step, that is, whether the subtree below the node contains a subtree of the same structure as tree B. So we went through the subtree below the node, and we got two sub nodes 9 and 2, which is exactly the same as the structure of tree B. At this point we found a subtree with the same structure as tree B in tree A, so the substructure of tree B and tree A.

In the interview, we must check the boundary conditions, that is, check the null pointer. When tree A or tree B is empty, the same output is defined. If you do not check and respond to the processing, the program is very easy to crash, this is the interview is very taboo things.

Here we use Java code to implement:

/**
 * Enter two binary trees A and B to determine if B is a substructure.
 * *
package swordforoffer;

Import Utils. Binarytreenode;

/**
 * @author Jinshuangqi
 * *
 * August 1, 2015/public
class E18substructureintree {
	public Boolean Hassubtree (Binarytreenode root1,binarytreenode root2) {
		if (Root2 = null) return
			true;
		if (ROOT1 = null) return
			false;
		Boolean result = false;
		
		if (root1!= null && root2!= null) {
			if (Root1.value = root2.value) Result
				= Doestree1havetree2 (ROOT1, ROOT2);
			if (!result) result
				= Hassubtree (root1.leftnode,root2);
			if (!result) result
				= Hassubtree (Root1.rightnode, Root2);
		}
		return result;
	}
	public boolean doestree1havetree2 (Binarytreenode root1,binarytreenode root2) {
		if (Root2 = null) return
			True ;
		if (ROOT1 = null) return
			false;
		if (Root1.value!= root2.value) return
			false;
		Return Doestree1havetree2 (Root1.leftnode,root2.leftnode) && doestree1havetree2 (Root1.rightnode, Root2.rightnode);
	}


We notice that the code above has multiple judgments about whether a pointer is null, in order to avoid the crash of the program by trying to access the null pointer, and to ask yourself whether this address is null or not if it is null when you need to access the address.

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.