bfs(find deepest nodes)->postorder(to gets smallest sub-tree)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n    public TreeNode subtreeWithAllDeepest(TreeNode root) {\n\n        //we first apply bfs to find the last layer nodes and store in a list.\n        // apply postorder left->right->node, so that we can find the smallest tree with all the deepest nodes\n// we first have to find the deepest level\n\n        List<Integer>s1 =new ArrayList<>();\n        Queue<TreeNode>queue = new LinkedList<>();\n         queue.offer(root);\n\n         while(!queue.isEmpty()){\n            List<Integer>s3 = new ArrayList<>();\n            int ll = queue.size();\n            for(int i=0;i<ll;i++){\n                TreeNode tar = queue.poll();\n                s3.add(tar.val);\n                if(tar.left!=null){\n                    queue.offer(tar.left);\n                }\n                if(tar.right!=null){\n                    queue.offer(tar.right);\n                }\n            }\n            if(queue.isEmpty()){\n                for(int r=0;r<s3.size();r++){\n                    s1.add(s3.get(r));\n                }\n            }\n         }\n\n         // now we have obtained our all the nodes in the list s1\n         //applying postorder to check the smallest tree\n         Queue<TreeNode>rt = new LinkedList<>();\n         postorder(root,rt,s1);\n\n         return rt.poll();\n    }\n\n    public void postorder(TreeNode root, Queue<TreeNode>s5,List<Integer>s3){\n        if(root==null){\n            return;\n        }\n\n        postorder(root.left,s5,s3);\n        postorder(root.right,s5,s3);\n        if(s5.isEmpty() && check(root,s3)){\n           s5.offer(root);\n        }\n    }\n\n    public boolean check(TreeNode root,List<Integer>s1){\n\n    List<Integer>ch = new ArrayList<>();\n    preorder(root,ch);\n     int cc =0; \n    for(int i=0;i<s1.size();i++){\n        int elm = s1.get(i);\n        if(ch.contains(elm)){\n            cc++;\n        }\n    }\n\n    return cc==s1.size();\n\n}\n\npublic void  preorder(TreeNode root,List<Integer>ch){\nif(root==null){\n    return;\n}\nch.add(root.val);\npreorder(root.left,ch);\npreorder(root.right,ch);\n}\n}\n```
['Java']