"Leetcode" Validate binary search tree two fork search trees judgment

Source: Internet
Author: User

Title: Given A binary tree, determine if it is a valid binary search tree (BST).

Knowledge Points: Features of BST:

1, all points of the left subtree of a node are less than or equal to the value of this point, the value of all nodes of the right subtree is greater than the value of that node;

2, the most left node value is the smallest, the most right node value is maximum;

3, the middle sequence traversal result value is a non-descending sequence

Question: If the use of Integer.max_value will not be able to test cases, but according to the definition in TreeNode, Val value is also int ah, no way to use a long, if who knows, trouble explain OH.

<span style= "FONT-SIZE:18PX;" >/** * Definition for Binary tree * public class TreeNode {*     int val, *     TreeNode left, *     TreeNode right;     TreeNode (int x) {val = x;} *} */public class Solution {public   Boolean isvalidbst (TreeNode root) {        return H Elper2 (Root, Long.max_value, long.min_value);    }    Private Boolean Helper2 (TreeNode root, long maxValue, long minValue) {        if (root = null) return true;        if (root.val >= maxValue | | root.val <= minValue) return false;        Return Helper2 (Root.left, Root.val, MinValue) && helper2 (Root.right, MaxValue, root.val);}    } </span>


Leetcode Validate binary search tree two fork find 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.