Press "Enter" to skip to content

Posts tagged as “sorting”

花花酱 LeetCode 530. Minimum Absolute Difference in BST

Link

Problem:

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

Example:

Note: There are at least two nodes in this BST.


Idea:

Sorting via inorder traversal gives us sorted values, compare current one with previous one to reduce space complexity from O(n) to O(h).

Solution:

C++ O(n) space

C++ O(h) space

Java

Python

Related Problems:

  • [解题报告] LeetCode 98. Validate Binary Search Tree

花花酱 LeetCode 154. Find Minimum in Rotated Sorted Array II

Problem:

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.

Idea: 

Divide and conquer

Time complexity:

Average: O(logn)

Worst: O(n)

Solution:

 

Related Problems:

[ZOJ] 3612: Median