public int getMinimumDifference(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
PriorityQueue<Integer> pq = new PriorityQueue<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
pq.offer(cur.val);
if (cur.left != null) queue.offer(cur.left);
if (cur.right != null) queue.offer(cur.right);
}
int pre = Integer.MAX_VALUE;
int min = Integer.MAX_VALUE;
while (!pq.isEmpty()) {
int cur = pq.poll();
min = Math.min(min, (int)Math.abs(curpre));
pre = cur;
}
return min;
}
Share my AC solution


@shawloatrchen Actually don't need to use
PriorityQueue
.InOrder
traverse aBST
will generate sorted values.
