This answer is so awesome!!
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { Integer min = null; public boolean isValidBST(TreeNode root) { if (root == null) { return true; } if (isValidBST(root.left) && (min == null || min < root.val)) { min = root.val; return isValidBST(root.right); } return false; } }
时间: 2024-10-25 05:34:49