Prove safety Offer: sub-tree (java version)

Title Description

Two binary inputs A, B, B is judged not substructure A's. (Ps: we agreed empty tree is not a tree any substructure)

analysis

When root1 and root2 not null, the judge began, directly or false
if found root1 in the corresponding root2 root, then enter the matching judgment, if exact match returns true.
If a match is not on, then to the left subtree root1 go, if it is not on the match, went to look for root1 right subtree.
The matching logic is:
if root2 traverse over, indicating characters match, return true;
if traversing over root1 described not match, return false;
if root1.val = root2.val, return false;!
Equal if the value of the current node, then also need to go and judge left subtree right subtree, each node is equal before returning true.

Recursive version

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
import java.util.*;
public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        boolean flag = false;
        // 当root1和root2都不为null时,才开始判断
        if(root1!=null && root2!=null){
            if(root1.val==root2.val){ // 找到了对应的根节点,进入判断
                flag=match(root1,root2);
            }
            if(!flag){ // 匹配失败,到root1的左子树去找
                flag=HasSubtree(root1.left,root2);
            }
            if(!flag){ // 匹配失败,再到root2的右子树去找
                flag=HasSubtree(root1.right,root2);
            }
        }
        return flag;
    }
    private boolean match(TreeNode root1,TreeNode root2){
        if(root2==null){
            return true;
        }
        if(root1==null){
            return false;
        }
        if(root1.val!=root2.val)
            return false;
        return match(root1.left,root2.left) && match(root1.right , root2.right);
    }
}

Preorder edition

import java.util.*;
public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root2==null)
            return false;
        Stack<TreeNode> stack1 = new Stack<>();
        if(root1!=null){
            stack1.push(root1);
            while(!stack1.isEmpty()){
                root1 = stack1.pop();
                if(root1.val==root2.val){ // 找到对应的根节点
                    if(match(root1,root2)){ // 进入判断,匹配上了就直接返回true
                        return true;
                    }
                }
                if(root1.right!=null){ // 否则继续遍历root1,再找下一个与root2相等的节点
                    stack1.push(root1.right);
                }
                if(root1.left!=null){
                    stack1.push(root1.left);
                }
            }
        }
        return false;
    }
    private boolean match(TreeNode root1,TreeNode root2){
        if(root2==null){
            return true;
        }
        if(root1==null){
            return false;
        }
        if(root1.val!=root2.val)
            return false;
        return match(root1.left,root2.left) && match(root1.right , root2.right);
    }
}

Guess you like

Origin blog.csdn.net/qq_43165002/article/details/90769846