[LeetCode] 98. Validate Binary Search Tree
2022/1/9 6:07:01
本文主要是介绍[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.
Example1:
Input: root = [2,1,3] Output: true
Example2:
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
所谓的搜索二叉树,就是一颗二叉树上每个子树的左子树的值都小于它,它的右子树都数值都大于它。根据这个定义,我们可以想到两个方法。
方法一:
二叉树中序遍历(左根右), 得到一个数组,这个数组单调递增,那么根据定义,就一定是搜索二叉树。
/** * 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) { if (root == null) { return true; } List<Integer> rst = new ArrayList<>(); helper(root, rst); return isIncrease(rst); } private void helper(TreeNode root, List<Integer> rst) { if (root == null) { return; } helper(root.left, rst); rst.add(root.val); helper(root.right, rst); } private boolean isIncrease(List<Integer> rst) { for (int i = 0; i < rst.size() - 1; i++) { if (rst.get(i) >= rst.get(i + 1)) { return false; } } return true; } }
第二个思路,就是用基本的递归。但是因为需要判断每层左子树是不是都小于根,右子树是不是都大于根,所以我们需要三个信息。
1)是不是平衡 => 左边的子树是不是平衡+右边子树是不是平衡+该层是不是平衡
2)左子树的最大值 => 只要最大值小于根就所有都小于
3)右子树的最小值 => 只要最小值大于根就所有都大于
/** * 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; * } * } */ public class Info { int leftMax; int rightMin; boolean isValid; Info(int leftMax, int rightMin, boolean isValid) { this.leftMax = leftMax; this.rightMin = rightMin; this.isValid = isValid; } } class Solution { public boolean isValidBST(TreeNode root) { if (root == null) { return true; } Info rst = helper(root); return rst.isValid; } private Info helper(TreeNode root) { if (root == null) { return null; } if (root.left == null && root.right == null) { return new Info(root.val, root.val, true); } Info l = helper(root.left); Info r = helper(root.right); int leftMax = root.val; int rightMin = root.val; boolean isValid = true; if ( l != null) { isValid = l.leftMax < root.val && l.isValid && isValid; leftMax = Math.max(l.leftMax, leftMax); rightMin = Math.min(l.rightMin, rightMin); } if (r != null) { isValid = r.rightMin > root.val && r.isValid && isValid; rightMin = Math.min(r.rightMin, rightMin); leftMax = Math.max(r.leftMax, leftMax); } return new Info(leftMax, rightMin, isValid); } }
这篇关于[LeetCode] 98. Validate Binary Search Tree的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-27Nacos多环境配置学习入门
- 2024-12-27Nacos快速入门学习入门
- 2024-12-27Nacos快速入门学习入门
- 2024-12-27Nacos配置中心学习入门指南
- 2024-12-27Nacos配置中心学习入门
- 2024-12-27Nacos做项目隔离学习入门
- 2024-12-27Nacos做项目隔离学习入门
- 2024-12-27Nacos初识学习入门:轻松掌握服务发现与配置管理
- 2024-12-27Nacos初识学习入门:轻松掌握Nacos基础操作
- 2024-12-27Nacos多环境配置学习入门