Title: Given A binary tree, determine if it is a valid binary search tree (BST).
Knowledge Points: Features of BST:
1, all points of the left subtree of a node are less than or equal to the value of this point, the value of all nodes of the right subtree is greater than the value of that node;
2, the most left node value is the smallest, the most right node value is maximum;
3, the middle sequence traversal result value is a non-descending sequence
Question: If the use of Integer.max_value will not be able to test cases, but according to the definition in TreeNode, Val value is also int ah, no way to use a long, if who knows, trouble explain OH.
<span style= "FONT-SIZE:18PX;" >/** * Definition for Binary tree * public class TreeNode {* int val, * TreeNode left, * TreeNode right; TreeNode (int x) {val = x;} *} */public class Solution {public Boolean isvalidbst (TreeNode root) { return H Elper2 (Root, Long.max_value, long.min_value); } Private Boolean Helper2 (TreeNode root, long maxValue, long minValue) { if (root = null) return true; if (root.val >= maxValue | | root.val <= minValue) return false; Return Helper2 (Root.left, Root.val, MinValue) && helper2 (Root.right, MaxValue, root.val);} } </span>
Leetcode Validate binary search tree two fork find trees