Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a 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 is binary search trees.
Example
An example:
2 / 1 3 / 4 5
The above binary tree is serialized as {2,1,3,#,#,4,#,#,5} (on level order).
Solution 1:
First think of the middle sequence traversal, due to the special BST, so the middle sequence of BST must be ascending, so as long as the first point of consideration of the pre-pass value is larger than the current value, it can be judged.
To think of the local, that is, if there is a left son, then the value of the left son must be less than the root value to meet the condition.
The code is as follows
public class Solution { /** * @param root:the root of binary tree. * @return: True If the binary tree is BST, or false */ private TreeNode pre = NULL; public Boolean isvalidbst (TreeNode root) { if (root = null) { return true; } Determine if the left is not if (!isvalidbst (Root.left)) { return false; } Determine if the entire tree and root section on the left is the if (pre! = null && pre.val >= root.val) { return false; } Judging right is not pre = root; if (!isvalidbst (root.right)) { return false; } return true; }}
Solution 2:
Divide & Conquer Divide and conquer law:
The first thing you need to do to analyze the problem is what the parameters are: the relationship between the current value and the left and right son values, and whether the return value is BST. When considering the relationship between the current value and the value of the left and right son, do not have to consider the size of all the son's value, just need to find their maximum value and the minimum value can be compared, so the parameters to be used to become: MinValue, MaxValue, and return value isbst of the subtree. A resulttype is defined, and the inside three parameters, Min value, max value, are BST.
At the time of the recursion, my idea is not to consider the conditions of termination, first assume that it is not a problem to consider this time and constantly recursive return what, to put what value to the next level.
In this problem, each layer of recursion must first divide,right and left, and then merge with conquer thought results into the next layer.
Now consider the termination condition, according to test instructions, 1, left subtree, right subtree has a not BST, the entire tree is not BST, return false at the same time, the Min max value is 0, because the return false is aborted. 2, Saozi right subtree is BST but left dial hand tree maximum is greater than root value, or the right subtree minimum is less than the root value.
Note that when handling illegal input, if the maximum value is required, the minimum value must be preserved when processing the boundary condition, and conversely, the minimum value is required to return the maximum value, which is the best value to avoid the return value being the last result when the input is illegal.
Specific implementation See Code:
class resulttype{int minValue; int maxValue; Boolean isbst; Resulttype (boolean isbst, int minValue, int maxValue) {this.isbst = Isbst; This.minvalue = MinValue; This.maxvalue = MaxValue; }}public class Solution {/** * @param root:the root of binary tree. * @return: True If the binary tree is BST, or false */public Boolean isvalidbst (TreeNode root) {Resulttype result = helper (root); return RESULT.ISBST; } Private Resulttype Helper (TreeNode root) {if (root = null) {return new Resulttype (True, integer.ma X_value, Integer.min_value); }//divide part Resulttype left = helper (root.left); Resulttype right = helper (root.right); Stop condition if (!left.isbst | |!right.isbst) {//Result false, Min max value does not matter return new Resulttype (FAL SE, 0, 0); } if (Root.left! = null && left.maxvalue >= root.val | | Root.right! = NULL && Right.minvalue <= root.val) {return new Resulttype (false, 0, 0); //conquer part: Pass the maximum minimum value to the next level return to new Resulttype (True, Math.min (Root.val, Left.minvalue), Math.max (Root.va L, Right.maxvalue)); }}
[Lintcode] Validate Binary Search Tree