[LeetCode-interview algorithm classic-Java implementation] [098-Validate Binary Search Tree (verify Binary Search Tree)], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [098-Validate Binary Search Tree (verify Binary Search Tree)], leetcode -- java
[098-Validate Binary Search Tree (verify Binary Search Tree )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]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 be binary search trees.

Theme

Verify Binary Search Tree

Solutions

The binary search tree is traversed in the middle order, and the results are saved in order. For the binary search tree in the middle order, the result is sorted in ascending order, and there are no many elements, therefore, you can determine whether a tree is a binary search tree.

Code Implementation

Tree node class

public class TreeNode {    int val;    TreeNode left;    TreeNode right;    TreeNode(int x) { val = x; }}

Algorithm Implementation class

Import java. util. stack; public class Solution {private Stack <Integer> stack; public boolean isValidBST (TreeNode root) {if (root = null) {return true ;} stack = new Stack <> (); inOrder (root); int I = stack. pop (); int j; while (! Stack. isEmpty () {j = stack. pop (); if (I <= j) {return false;} I = j;} return true ;} /*** 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 Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/47310557]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.