检查是否是子树?

Check if subtree?

给定两个二叉树,其头标为 T 和 S,最多有 N 个节点。任务是检查 S 是否作为子树存在于 T 中。 树 T1 的子树是树 T2,由 T1 中的一个节点及其在 T1 中的所有后代组成。

为什么我的方法失败了?

我的算法是:- 找到 T 的中序和前序遍历,将它们存储在两个列表中。 找到 S 的中序和前序遍历,将它们存储在两个列表中。 如果 T 的中序列表和前序列表出现在 S 的中序列表和前序列表中,则 return true else false.


import java.util.LinkedList; 
import java.util.Queue; 
import java.io.*;
import java.util.*;

class Node{
    int data;
    Node left;
    Node right;
    Node(int data){
        this.data = data;
        left=null;
        right=null;
    }
}

class GfG {
    
    static Node buildTree(String str){
        
        if(str.length()==0 || str.charAt(0)=='N'){
            return null;
        }
        
        String ip[] = str.split(" ");
        // Create the root of the tree
        Node root = new Node(Integer.parseInt(ip[0]));
        // Push the root to the queue
        
        Queue<Node> queue = new LinkedList<>(); 
        
        queue.add(root);
        // Starting from the second element
        
        int i = 1;
        while(queue.size()>0 && i < ip.length) {
                
            // Get and remove the front of the queue
            Node currNode = queue.peek();
            queue.remove();
                
            // Get the current node's value from the string
            String currVal = ip[i];
                
            // If the left child is not null
            if(!currVal.equals("N")) {
                    
                // Create the left child for the current node
                currNode.left = new Node(Integer.parseInt(currVal));
                // Push it to the queue
                queue.add(currNode.left);
            }
                
            // For the right child
            i++;
            if(i >= ip.length)
                break;
                
            currVal = ip[i];
                
            // If the right child is not null
            if(!currVal.equals("N")) {
                    
                // Create the right child for the current node
                currNode.right = new Node(Integer.parseInt(currVal));
                    
                // Push it to the queue
                queue.add(currNode.right);
            }
            i++;
        }
        
        return root;
    }
    static void printInorder(Node root){
        if(root == null)
            return;
            
        printInorder(root.left);
        System.out.print(root.data+" ");
        
        printInorder(root.right);
    }
    
    public static void main (String[] args) throws IOException {
            BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
            
            int t=Integer.parseInt(br.readLine());
            while(t-- > 0){
                String tt= br.readLine();
                Node rootT = buildTree(tt);
                
                String s= br.readLine();
                Node rootS = buildTree(s);
               // printInorder(root);
                
                Solution tr=new Solution();
                boolean st=tr.isSubtree(rootT, rootS);
                if(st==true){
                    System.out.println("1");
                }else{
                    System.out.println("0");
                }
            }
    }
}// } Driver Code Ends



class Solution {
    // algo implementation is started from here.
public static void preorder(Node root , ArrayList<Integer>al )
{
    if(root!=null)
    {
    al.add(root.data);
    preorder(root.left, al);
    preorder(root.right, al);
    }
}

public static void inorder(Node root, ArrayList<Integer>al)
{
    
  if(root!=null)
    {
    
    inorder(root.left, al);
    al.add(root.data);
    inorder(root.right, al);  
    }
    
}

    public static boolean isSubtree(Node t, Node s) 
    {
        ArrayList<Integer> alt1 = new ArrayList<>();
        ArrayList<Integer>alt2 = new ArrayList<>();
        
        ArrayList<Integer> als1 = new ArrayList<>();
        ArrayList<Integer>als2 = new ArrayList<>();
  
    preorder(t,alt1);
    inorder(t,alt2);
    
    preorder(s,als1);
    inorder(s,als2);
    
if(alt1.containsAll(als1) && alt2.contains(als2))
return true;
 
    
    
    return false;
    
    
    
         
    }
}
~~~

您的方法是正确的,您正在检查 S 的数组列表是否包含 T 的数组列表中存在的所有值 只是改变这部分 als1.containsAll(alt1) && als2.contains(alt2)if(alt1.containsAll(als1) && alt2.contains(als2)) return true;