public class TreeLevel { public ListNode getTreeLevel(TreeNode root, int dep) { // write code here Listresult = new ArrayList(); ListNode res = null; if(root == null || dep <= 0 ){ return res; } result.add(root); if(dep == 1){ res = new ListNode(root.val); return res; } while(dep > 1){ List temp = new ArrayList(); for(TreeNode t : result){ if(t.left != null) temp.add(t.left); if(t.right != null) temp.add(t.right); } result = temp; dep--; } res = new ListNode(result.get(0).val); ListNode temp = res; for(int i = 1; i < result.size(); i++){ temp.next = new ListNode(result.get(i).val); temp = temp.next; } return res; }}