"Leetcode-Interview algorithm classic-java implementation" "098-validate Binary search tree (verify binary searching trees)"

Source: Internet
Author: User

"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)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.