https://oj.leetcode.com/problems/validate-binary-search-tree/
http://blog.csdn.net/linhuanmars/article/details/23810735
/** * definition for binary tree * public class treenode { * int val; * TreeNode left; * treenode right; * treenode (int &NBSP;X) { val = x; } * } */public class solution { public boolean isvalidbst (treenode root) { // a BST requirement // left Tree also BST, left Max less current value // Right tree also BST, right minimum greater current value if (root == null) return true; return getvalues (Root) .valid; } private values getvalues (Treenode node) { int min = node.val; int max = node.val; boolean valid = true; if (node.left != null) { Values left = GetValues (node.left); valid &= left.valid; valid &= (left.max < Node.val); if (valid) min = math.min ( Left.min, min); } if (node.right != null) { Values right = getvalues (node.right); valid &= right.valid; valid &= (Right.min > node.val); if (valid) max = math.max (Right.max, max); } return new values (min, max, valid); } static class Values { values (int min, int max, boolean valid) { this.min = min; this.max = max; this.valid = valid; } int min; int max; boolean valid; }}
[leetcode]98 Validate Binary Search Tree