/* * 272. Closest Binary Search Tree Value II * 2016-7-21 by Mingyang * 一次过,dfs和list */ public ListclosestKValues(TreeNode root, double target, int k) { List list=new ArrayList (); if(root==null) return list; Stack stack=new Stack (); TreeNode p=root; while(!stack.isEmpty()||p!=null){ if(p!=null){ stack.push(p); p=p.left; }else{ TreeNode q=stack.pop(); if(list.size() Math.abs(q.val-target)){ list.remove(0); list.add(q.val); }else{ return list; } } p=q.right; } } return list; }