Sword finger offer: substructure of tree (Java)

Keywords: Java

1. Problem description

Input two binary trees a and B to determine whether B is a substructure of A. (ps: we agree that an empty tree is not a substructure of any tree)

2. way of thinking

First, according to the topic, you need to judge only when both trees are not null. Otherwise, you can directly return false.
Then it is to traverse the big tree and find the head node of the small tree. If it is not found after traversing, it is false.
If you find the head node of a small tree, compare the corresponding left and right child nodes in turn. If there is an inequality, it is false.
One by one, it must be that the node of the small tree is null at the end, and the big tree is not necessarily null, so the small tree is used to judge first.
If the big tree is null and the small tree is not, it means that the small tree is not the substructure of the big tree. Return false.

What I find difficult is how to return to the root node after the left subtree is traversed in the process of traversing the tree. Here are two methods. In the second method, I will recursively traverse the subtree on the side of the root node. After the traversal, I can return to the root node in the first method.

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        boolean result = false;    //Used to record judgment results
        if(root1 != null && root2 != null){    //Only when both root nodes are not null can the judgment be made, otherwise false will be returned directly.
                result = doesTree1HaveTree2(root1,root2);    //Call the doesTree1HaveTree2 method to determine whether Tree2 is a substructure of Tree1
            if(!result){    //If the values of the current two root nodes are not equal, it is determined whether the left child node of root1 is equal to the root2 node.
                result = doesTree1HaveTree2(root1.left,root2);
            }
            if(!result){    //If the nodes of the left subtree of root1 are not equal to those of root2, judge whether the nodes of the right subtree of root1 and root2 are equal.
                result = doesTree1HaveTree2(root1.right, root2);
            }
        }
        return result;    //root1 will return results after traversal.
    }
    
    public boolean doesTree1HaveTree2(TreeNode node1, TreeNode node2){
        if(node2 == null){    //If the node2 tree is traversed first and all of them are paired with node1, it means that node2 is the substructure of node1 and returns true.
            return true;
        }
        if(node1 == null){    //If node2 is not null and node1 is null, and the name node2 is not a substructure of node1, return false
            return false;
        }
        if(node1.val != node2.val){    //As long as one doesn't want to wait to return to false directly during the judgment process, note here that it is only to judge whether a subtree does not match. In HasSubTree, it is also to judge whether there is root2 structure in the right subtree, so it won't be missed.
            return false;
        }
        return doesTree1HaveTree2(node1.left, node2.left) && doesTree1HaveTree2(node1.right, node2.right);
        //If the value of the current node is equal, judge whether the value of the left and right child nodes are equal. If one is not equal, return false.
    }
}

Be careful:

(1) in the doestree1havetree2 method, whether the small tree is null or not must be put in the front. It cannot be exchanged with whether the big tree is null or not. Because if the tree is null, the small tree is not sure. If the small tree is null, it should be true. At this time, it returns false, which is an error.
(2) if there is a part of the small tree in the left subtree of the root node and the whole small tree in the right subtree, this situation will not be missed, because in the HasSubTree method, the left subtree is judged first. When the right subtree is judged and the left subtree returns false, the root node of the small tree will be found again in the right subtree, so it will not be missed.

Reference resources
Boooobby's answer

Posted by blt2589 on Fri, 18 Oct 2019 13:28:27 -0700