package leetcode;

/**
 * https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/
 */
public class Problem865 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public TreeNode subtreeWithAllDeepest(TreeNode root) {
        // TODO
        return null;
    }

    public static void main(String[] args) {

    }
}

@@ -12,16 +12,157 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Queue;%0Aimport java.util.stream.Collectors;%0A%0A
 /**%0A * h
@@ -541,48 +541,2559 @@
    p
-ublic static void main(String%5B%5D args) %7B%0A
+rivate static TreeNode deserialize(String str) %7B%0A        String%5B%5D nodes = str.substring(1, str.length() - 1).split(%22,%22);%0A        Queue%3CTreeNode%3E queue = new LinkedList%3C%3E();%0A        int idx = 0;%0A        String val = nodes%5Bidx%5D;%0A        if (val.isEmpty()) %7B%0A            return null;%0A        %7D%0A        TreeNode root = null;%0A        queue.add(new TreeNode(Integer.parseInt(val)));%0A        while (!queue.isEmpty()) %7B%0A            TreeNode parent = queue.remove();%0A            if (idx == 0) %7B%0A                root = parent;%0A            %7D%0A            if (++idx %3C nodes.length) %7B%0A                if (!nodes%5Bidx%5D.equals(%22null%22)) %7B%0A                    parent.left = new TreeNode(Integer.parseInt(nodes%5Bidx%5D));%0A                    queue.add(parent.left);%0A                %7D%0A            %7D%0A            if (++idx %3C nodes.length) %7B%0A                if (!nodes%5Bidx%5D.equals(%22null%22)) %7B%0A                    parent.right = new TreeNode(Integer.parseInt(nodes%5Bidx%5D));%0A                    queue.add(parent.right);%0A                %7D%0A            %7D%0A        %7D%0A        return root;%0A    %7D%0A%0A    public static String serialize(TreeNode node) %7B%0A        if (node == null) %7B%0A            return %22%5B%5D%22;%0A        %7D%0A        List%3CString%3E nodes = new ArrayList%3C%3E();%0A        Queue%3CTreeNode%3E queue = new LinkedList%3C%3E();%0A        queue.add(node);%0A        nodes.add(Integer.toString(node.val));%0A        while (!queue.isEmpty()) %7B%0A            TreeNode parent = queue.remove();%0A            if (parent.left == null) %7B%0A                nodes.add(%22null%22);%0A            %7D else %7B%0A                queue.add(parent.left);%0A                nodes.add(Integer.toString(parent.left.val));%0A            %7D%0A            if (parent.right == null) %7B%0A                nodes.add(%22null%22);%0A            %7D else %7B%0A                queue.add(parent.right);%0A                nodes.add(Integer.toString(parent.right.val));%0A            %7D%0A        %7D%0A        // remove all the nulls from behind%0A        int i = nodes.size() - 1;%0A        while (nodes.get(i).equals(%22null%22)) %7B%0A            i--;%0A        %7D%0A        nodes = nodes.subList(0, i + 1);%0A        return %22%5B%22 + nodes.stream().collect(Collectors.joining(%22,%22)) + %22%5D%22;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem865 prob = new Problem865();%0A        System.out.println(serialize(%0A            prob.subtreeWithAllDeepest(deserialize(%0A                %22%5B3,5,1,6,2,0,8,null,null,7,4%5D%22)))); // %5B2,7,4%5D%0A        System.out.println(serialize(%0A            prob.subtreeWithAllDeepest(deserialize(%0A                %22%5B3,5,1,6,2,0,8,null,null,7,4,null,null,null,null,9%5D%22)))); // %5B9%5D
 %0A   

Update problem 865