Share my AC solution


  • 0
    S
    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(cur-pre));
    		pre = cur;
    	}
    	return min;
    }
    

  • 0

    @shawloatrchen Actually don't need to use PriorityQueue. In-Order traverse a BST will generate sorted values.


  • 0
    S

    @shawngao Yes, you're right


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.