Tag: item does not contain IMG Sub [] 9.png nod End ring
The topic describes the input of two binary trees, a, a, B, to determine whether a sub-structure of a. (PS: We agree that an empty tree is not a sub-structure of any tree) topic code
/*** Enter two binary trees A, B, to determine whether the sub-structure of a. (PS: We agree that the empty tree is not a sub-structure of any tree) * Created by Yukai Fan on 2018/9/3. */ Public classHassubtree { Public Static voidMain (string[] args) {TreeNode a=NewTreeNode (2); A.left=NewTreeNode (4); A.right=NewTreeNode (6); A.left.left=NewTreeNode (7); A.left.right=NewTreeNode (9); A.right.right=NewTreeNode (21); A.right.left=NewTreeNode (14); TreeNode A1=NewTreeNode (2); A1.left=NewTreeNode (4); A1.right=NewTreeNode (6); Booleanb =Hassubtree (A, A1); System.out.println (b); } Public Static BooleanHassubtree (TreeNode root1, TreeNode root2) {Booleanresult =false; //Judging premise Two trees are non-empty if(Root1! =NULL&& Root2! =NULL) { //if the node that exists ROOT1 is equal to the root node of Root2 if(Root1.val = =root2.val) {//The root node of the Root2 is the starting point to determine whether it contains Root2result =Issubtree (ROOT1, Root2); } //If you can't find it, go to Root1 's left subtree loop recursion. if(!result) {Result=Hassubtree (Root1.left, Root2); } //if Zuozi can not find it, go to Root1 's right subtree loop recursion if(!result) {Result=Hassubtree (Root1.right, Root2); } } returnresult; } Public Static BooleanIssubtree (TreeNode root1, TreeNode root2) {//returns True if the Root2 has been traversed in root1 . if(Root2 = =NULL) { return true; } //If ROOT1 is finished, and Root2 is not finished, the ROOT1 does not contain Root2 and returns false if(ROOT1 = =NULL) { return false; } //returns False if one of the points does not correspond on if(Root1.val! =root2.val) {return false; } //if the root node corresponds, go to the child node and match it separately . returnIssubtree (Root1.left, Root2.left) &&Issubtree (Root1.right, root2.right); }}
/***/Publicclass TreeNode {int val; TreeNode left; TreeNode right; TreeNode (int val) { this. val = val; }}
Topic extension
Sub-tree and substructure of binary tree
A subtree means that as long as a node is included, all nodes under that node must be included
The sub-structure means that it contains a node that can take the left subtree or the right subtree, or not take
Simply put, the substructure can be any part of the tree.
Here's a simple example:
such as 1 is 7 nodes, height is 3 of the two fork tree
Figure 1:
The subtree in Figure 1 can be:
The sub-structure of Figure 1 is:
Since the substructure can be any part of the original tree, Figure 4 is a sub-structure.
How to judge the sub-tree of a binary tree
Analysis: Because subtree indicates that if a node is included, all nodes under the node must be included. (so 4, not a tree's subtree.) But sub-structure!!! )
So the code modifies the Code on the Issubtree () based on the above
Public Static BooleanisSubTree2 (TreeNode root1, TreeNode root2) {/*because subtree indicates that if a node is included, all nodes under the node must be included. So only if Root1 and Root2 both traverse the end are empty, can return true*/ if(ROOT1 = =NULL&& Root2 = =NULL) { return true; } Else if(Root1! =NULL&& Root2! =NULL) { if(Root1.val! =root2.val) {return false; } returnIsSubTree2 (Root1.left,root2.left) &&isSubTree2 (Root1.right, root2.right); } Else { return false; } }
Java BASIC Programming--Tree substructure