Same as LCA O(2N) 
1.get deepest nodes\n2.use LCA \nTime O(N) space O(N)\nyou can reduce space by using just one set\n\n    public TreeNode subtreeWithAllDeepest(TreeNode root) {\
1.get deepest nodes\n2.use LCA \nTime O(N) space O(N)\nyou can reduce space by using just one set\n\n    public TreeNode subtreeWithAllDeepest(TreeNode root) {\n        Map<Integer, Set<TreeNode>> map = new HashMap<>();\n        Queue<TreeNode> queue = new LinkedList<>();\n        int level = 0;\n        queue.offer(root);\n        \n        while(!queue.isEmpty()) {\n            int size = queue.size();\n            Set<TreeNode> list = new HashSet<>();\n            \n            for(int i = 0; i < size; i++) {\n                TreeNode current = queue.poll();\n                list.add(current);\n                \n                if(current.left != null) {\n                    queue.offer(current.left);\n                }\n                \n                if(current.right != null) {\n                    queue.offer(current.right);\n                }\n            }\n            map.put(level, list);\n            level++;\n        }\n        Set<TreeNode> nodes = map.get(level - 1);\n        return LCA(root, nodes);     \n    }\n    \n    private TreeNode LCA(TreeNode root, Set<TreeNode> set) {\n        if(root == null ||set.contains(root)) {\n            return root;\n        }\n        \n        TreeNode left = LCA(root.left, set);\n        TreeNode right = LCA(root.right, set);\n        \n        if(left != null && right != null) {\n            return root;\n        }\n        \n        return left == null ? right : left;\n    }\n
[]