"098-validate binary search Tree"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
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.
Main Topic
Verify binary search tree
Thinking of solving problems
The two-fork search tree is sequenced, the results are saved sequentially, and the result of the sequential traversal of the binary search tree is a small-to-large sequence, and there is no heavy elements, which can be used to determine whether a tree is a two-fork search tree.
Code Implementation
Tree Node class
publicclass TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}
Algorithm implementation class
ImportJava.util.Stack; Public class solution { PrivateStack<integer> Stack; Public Boolean Isvalidbst(TreeNode Root) {if(Root = =NULL) {return true; } stack =NewStack<> (); Inorder (root);inti = Stack.pop ();intJ while(!stack.isempty ()) {j = Stack.pop ();if(I <= J) {return false; } i = J; }return true; }/** * If it is a binary search tree must be ordered * @param root * / Public void inorder(TreeNode Root) {if(Root! =NULL) {inorder (root.left); Stack.push (Root.val); Inorder (Root.right); } }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47310557"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"leetcode-Interview algorithm classic-java implementation" "098-validate binary search tree (validating binary searching trees)"