[LeetCode] 98. Validate Binary Search Tree

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

A valid 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:
Example 1
Input: root = [2,1,3]
Output: true

Example 2:
Example 2
Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation: The root node’s value is 5 but its right child’s value is 4.

Constraints:
The number of nodes in the tree is in the range [1, 104].
-231 <= Node.val <= 231 - 1

验证二叉搜索树。

给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。

有效 二叉搜索树定义如下:
节点的左子树只包含 小于 当前节点的数。
节点的右子树只包含 大于 当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/validate-binary-search-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

二叉搜索树的特性是对于每个node而言,他的左子树上任意节点都比他自身小,右子树上任意节点都比他自身大。这个题也是有两种做法,迭代和递归。时间空间复杂度都是O(n)。

复杂度

时间O(n)
空间O(n)

迭代

迭代的做法其实就是树的中序遍历,需要用到 stack。按照中序遍历的顺序遍历所有的 node,记录一个 pre node 和一个 currrent node。如果树是一个合法的BST,中序遍历的结果应该是有序的。

代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
// corner case
if (root == null) {
return true;
}

// normal case
TreeNode cur = root;
TreeNode pre = null;
Stack<TreeNode> stack = new Stack<>();
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
if (pre != null && pre.val >= cur.val) {
return false;
}
pre = cur;
cur = cur.right;
}
return true;
}
}

JavaScript实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function (root) {
if (root === null) return true;
let stack = [];
let cur = root;
let pre = null;
while (cur !== null || stack.length) {
while (cur !== null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
if (pre !== null && cur.val <= pre.val) {
return false;
}
pre = cur;
cur = cur.right;
}
return true;
};

递归

递归的代码就非常简单了,任何节点如果存在左孩子,左孩子的 val 不能大于其父节点;如果存在右孩子, 右孩子的 val 不能小于其父节点。

代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
return helper(root, null, null);
}

private boolean helper(TreeNode root, Integer min, Integer max) {
if (root == null) return true;
if (min != null && root.val <= min) return false;
if (max != null && root.val >= max) return false;
return helper(root.left, min, root.val) && helper(root.right, root.val, max);
}
}

JavaScript实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function (root) {
if (root === null) return true;
return helper(root, null, null);
};

var helper = function (root, min, max) {
if (root === null) return true;
if (min !== null && root.val <= min) return false;
if (max !== null && root.val >= max) return false;
return helper(root.left, min, root.val) && helper(root.right, root.val, max);
}

[LeetCode] 98. Validate Binary Search Tree
https://shurui91.github.io/posts/1121020247.html
Author
Aaron Liu
Posted on
January 31, 2020
Licensed under