Validate Binary Search Tree

First solution:

Inorder traverse is a better solution.

use a arraylist to store the inorder traverse and check whether the vals are in increasing order, 或者可以直接将当前root的值与list的最后一个元素相比

much simpler solution:

also dfs:

use Long.MINVALUE and Long.MAX_VALUE incase there are only one node in the tree and its val is same as Integer.MIN_VALUE or Integer.MAXVALUE, 上述方法本质上都是做一次树的遍历,所以时间复杂度是O(n),对于最后一种方法来说空间复杂度是O(logn),因为每一层我们都create了一个min,和max value,而logn正表示树的深度。

results matching ""

    No results matching ""