简体   繁体   English

使用 dfs 确定给定树是否是子树

[英]Determine if given tree is a subtree using dfs

I'm trying to determine if one tree (t) is a subtree of another tree (s).我试图确定一棵树(t)是否是另一棵树(s)的子树。

This is a link to the leetcode which explains the problem thoroughly: https://leetcode.com/problems/subtree-of-another-tree/这是指向彻底解释问题的 leetcode 的链接: https ://leetcode.com/problems/subtree-of-another-tree/

My approach: I have one function that does a dfs on s and compares each node to the root of t in another function to determine if t is a subtree of s我的方法:我有一个函数对 s 执行 dfs 并将每个节点与另一个函数中的 t 的根进行比较以确定 t 是否是 s 的子树

My solution doesn't work for when s=[1,1] and t=[1], although I think it should be working.我的解决方案在 s=[1,1] 和 t=[1] 时不起作用,尽管我认为它应该有效。 Could you please look at my code and explain what's wrong.你能看看我的代码并解释什么是错的。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSubtree(TreeNode s, TreeNode t) {
        /* dfs on s, at each node running a compare tree function for s at that node and
        root of t*/
        
        if(s == null || t == null) {
            return false;
        }
        
        return dfs(s, t);
    }
    
    public static boolean dfs(TreeNode s, TreeNode t) {
        if(s == null) {
            return false;
        }
        
        if(s.val == t.val) {
            return isSameTree(s, t);
        }
        
        
        return dfs(s.left, t) || dfs(s.right, t);
    }
    
    public static boolean isSameTree(TreeNode s, TreeNode t) {
        if(s == null || t == null) {
            return s == t;
        }
        
        if(s.val != t.val) {
            return false;
        }
        
        return isSameTree(s.left, t.left) && isSameTree(s.right, t.right);
    }
}

You need to check for all nodes of s if t is a subtree of that node or not.如果t是该节点的子树,则需要检查s所有节点。 If you stop at first node while performing dfs on s and its value is same as root of t but subtrees are different, there might be some another node of tree s whose value and subtree both are same as t .如果在s上执行 dfs 时在s一个节点处停止并且其值与t根相同但子树不同,则可能存在树s的另一个节点,其值和子树都与t相同。

In other words, you need to repeat your 1st step (perform dfs on s and compare each node of s to the root of t ) until you have checked all nodes of s (dfs is complete on s ) or found that t is subtree of s .换句话说,你需要重复你的第一步(执行DFS的s和中的每个节点比较s到根t ),直到你已经检查的所有节点s (DFS完成的s ),或发现, t是子树s

 st (1) (1) / (1)

Do not return from root of s just because of same values of root of s and t.不要仅仅因为 s 和 t 的根值相同就从 s 的根返回。 If t is not subtree of that node, keep doing dfs to find another node whose value and subtree both are same as t (left child of root of s in this case).如果 t 不是该节点的子树,则继续执行 dfs 以查找其值和子树都与 t 相同的另一个节点(在这种情况下是 s 根的左子节点)。

For more clarity, below is your code with corrected part highlighted:为了更清楚,下面是您的代码,其中突出显示了更正的部分:

class Solution {
    public boolean isSubtree(TreeNode s, TreeNode t) {
        /* dfs on s, at each node running a compare tree function for s at that node and
        root of t*/
        
        if(s == null || t == null) {
            return false;
        }
        
        return dfs(s, t);
    }
    
    public static boolean dfs(TreeNode s, TreeNode t) {
        if(s == null) {
            return false;
        }
        
        // ==== Corrected below if ====
        // apart from s.val == t.val, if isSameTree(s, t) is true at this
        // point, return true; otherwise keep doing dfs for rest of the tree s
        // other same value node of s can be the answer
        if(s.val == t.val && isSameTree(s, t)) {
            return true;
        }
        
        
        return dfs(s.left, t) || dfs(s.right, t);
    }
    
    public static boolean isSameTree(TreeNode s, TreeNode t) {
        if(s == null || t == null) {
            return s == t;
        }
        
        if(s.val != t.val) {
            return false;
        }
        
        return isSameTree(s.left, t.left) && isSameTree(s.right, t.right);
    }
}

Looks pretty good!看起来不错! Almost there!差不多好了!

This'd pass:这会通过:

public class Solution {
    public static final boolean isSubtree(
        final TreeNode s,
        final TreeNode t
    ) {
        if (s == null) {
            return false;
        }

        if (checkNextLevel(s, t)) {
            return true;
        }

        return isSubtree(s.left, t) ||
               isSubtree(s.right, t);
    }

    private static final boolean checkNextLevel(
        final TreeNode s,
        final TreeNode t
    ) {
        if (s == null && t == null) {
            return true;
        }

        if (
            (s == null || t == null) ||
            (s.val != t.val)
        ) {
            return false;
        }


        return checkNextLevel(s.left, t.left) &&
               checkNextLevel(s.right, t.right);
    }
}

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

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