LeetCode - Medium - 98. Validate Binary Search Tree

迷南。 2022-10-06 10:52 134阅读 0赞

Topic

  • Tree
  • Recursion
  • Depth-first Search

Description

https://leetcode.com/problems/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:
3d85b39145ecc61874276bcb7cfe6765.png

  1. Input: root = [2,1,3]
  2. Output: true

Example 2:
f8c9b63a549daf28d19243f8141bb56d.png

  1. Input: root = [5,1,4,null,null,3,6]
  2. Output: false
  3. 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 , 1 0 4 ] [1, 10^4] [1,104].
  • − 2 31 < = N o d e . v a l < = 231 − 1 -2^{31} <= Node.val <= 2{31} - 1 −231<=Node.val<=231−1

Analysis

方法一:我写的,中序遍历模式递归版。

方法二:我写的,中序遍历模式迭代版。

方法三:别人写的,前序遍历模式递归版。

Submission

  1. import java.util.LinkedList;
  2. import com.lun.util.BinaryTree.TreeNode;
  3. public class ValidateBinarySearchTree {
  4. //方法一:我写的,中序遍历模式递归版
  5. public boolean isValidBST(TreeNode root) {
  6. Integer[] prev = { null};
  7. boolean[] result = { true};
  8. isValidBST(root, prev, result);
  9. return result[0];
  10. }
  11. private void isValidBST(TreeNode node, Integer[] prev, boolean[] result) {
  12. if(node == null || !result[0]) return;
  13. isValidBST(node.left, prev, result);
  14. if(prev[0] != null && prev[0] >= node.val) {
  15. result[0] = false;
  16. return;
  17. }
  18. prev[0] = node.val;
  19. isValidBST(node.right, prev, result);
  20. }
  21. //方法二:我写的,中序遍历模式迭代版
  22. public boolean isValidBST2(TreeNode root) {
  23. LinkedList<TreeNode> stack = new LinkedList<>();
  24. TreeNode p = root;
  25. Integer prev = null;
  26. while(!stack.isEmpty() || p != null) {
  27. if(p != null) {
  28. stack.push(p);
  29. p = p.left;
  30. }else {
  31. TreeNode node = stack.pop();
  32. if(prev != null && prev >= node.val)
  33. return false;
  34. prev = node.val;
  35. p = node.right;
  36. }
  37. }
  38. return true;
  39. }
  40. //方法三:别人写的,前序遍历模式递归版
  41. public boolean isValidBST3(TreeNode root) {
  42. return isValidBST3(root, null, null);
  43. }
  44. private boolean isValidBST3(TreeNode root, Integer min, Integer max) {
  45. if(root == null) return true;
  46. if(min != null && root.val <= min) return false;
  47. if(max != null && root.val >= max) return false;
  48. return isValidBST3(root.left, min, root.val) && isValidBST3(root.right, root.val, max);
  49. }
  50. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. import com.lun.util.BinaryTree;
  4. public class ValidateBinarySearchTreeTest {
  5. @Test
  6. public void test() {
  7. ValidateBinarySearchTree obj = new ValidateBinarySearchTree();
  8. assertTrue(obj.isValidBST(BinaryTree.integers2BinaryTree(2,1,3)));
  9. assertFalse(obj.isValidBST(BinaryTree.integers2BinaryTree(5,1,4,null,null,3,6)));
  10. assertFalse(obj.isValidBST(BinaryTree.integers2BinaryTree(5,4,6,null,null,3,7)));
  11. }
  12. @Test
  13. public void test2() {
  14. ValidateBinarySearchTree obj = new ValidateBinarySearchTree();
  15. assertTrue(obj.isValidBST2(BinaryTree.integers2BinaryTree(2,1,3)));
  16. assertFalse(obj.isValidBST2(BinaryTree.integers2BinaryTree(5,1,4,null,null,3,6)));
  17. assertFalse(obj.isValidBST2(BinaryTree.integers2BinaryTree(5,4,6,null,null,3,7)));
  18. }
  19. @Test
  20. public void test3() {
  21. ValidateBinarySearchTree obj = new ValidateBinarySearchTree();
  22. assertTrue(obj.isValidBST2(BinaryTree.integers2BinaryTree(2,1,3)));
  23. assertFalse(obj.isValidBST2(BinaryTree.integers2BinaryTree(5,1,4,null,null,3,6)));
  24. assertFalse(obj.isValidBST2(BinaryTree.integers2BinaryTree(5,4,6,null,null,3,7)));
  25. }
  26. }

发表评论

表情:
评论列表 (有 0 条评论,134人围观)

还没有评论,来说两句吧...

相关阅读