3 lines Java code, easy to understand


  • 0
    M
    public boolean isSubtree(TreeNode s, TreeNode t) {
            return serialize(s).contains(serialize(t));
        }
        private String serialize(TreeNode node) {
            if (node == null) return "#";
            return serialize(node.left) + node.val + serialize(node.right) + "/";
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.