繁体   English   中英

检查子树? , 提前谢谢

[英]Check if subtree? , Thanx in advance

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

为什么我的方法失败了?

我的算法是:- 查找 T 的中序和前序遍历,将它们存储在两个列表中。 查找 S 的中序和前序遍历,将它们存储在两个列表中。 如果 T 的中序和前序列表出现在 S 的中序和前序列表中,则返回 true,否则返回 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 的 arraylist 是否具有 T 的数组列表中存在的所有值只需将这部分als1.containsAll(alt1) && als2.contains(alt2)更改为if(alt1.containsAll(als1) && alt2.contains(als2)) return true;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM