98.Validate-Binary-Search-Tree

98. Validate Binary Search Tree

题目地址

https://leetcode.com/problems/validate-binary-search-tree/

http://www.lintcode.com/problem/validate-binary-search-tree/

http://www.jiuzhang.com/solutions/validate-binary-search-tree/

题目描述

Given a binary tree, determine if it is a valid binary search tree (BST).

Assume a BST is defined as follows:

The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.


Example 1:

    2
   / \
  1   3

Input: [2,1,3]
Output: true

代码

Approach #1 Divide & Conquer

  1. 传递upper和lower, 和当前节点进行比较

Complexity Analysis:

  • Time complexis: O(n)

  • Space complexity: O(N)

Approach #2 Iteration

Approach #3 No-Recursion, Access nodes from small to large

Last updated

Was this helpful?