Leetcode no. 98
来源:互联网 发布:sql 找不到指定的路径 编辑:程序博客网 时间:2024/05/23 12:02
98. Validate Binary Search Tree
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.
Example 1:
2 / \ 1 3Binary tree
[2,1,3]
, return true.Example 2:
1 / \ 2 3Binary tree
[1,2,3]
, return false./** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean isValidBST(TreeNode root) { return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE); } private boolean isValidBST(TreeNode root, long lower, long upper){ if (root==null) return true; if (root.val>=upper || root.val<=lower) return false; return isValidBST(root.left, lower, root.val) && isValidBST(root.right, root.val, upper); }}
0 0
- Leetcode no. 98
- Leetcode NO.98 Validate Binary Search Tree
- leetCode No.98 Validate Binary Search Tree
- LeetCode No.98 Validate Binary Search Tree
- LeetCode No.11 ~No.12
- Leetcode NO.78 Subsets
- Leetcode NO.77 Combinations
- Leetcode NO.46 Permutations
- 【leetcode】No. 235 LCABST
- LeetCode no. 35
- Leetcode no. 37
- Leetcode no. 279
- Leetcode no. 335
- Leetcode no. 283
- Leetcode no. 181 (sql)
- Leetcode no. 40
- Leetcode no. 238
- Leetcode no. 26
- SVN客户端安装和服务器搭建
- Html最详细语法
- 淘忆项目之主页界面客户端的整理归纳
- SSH项目环境搭建
- 了解build.gradle
- Leetcode no. 98
- mysql logstash 配置
- POJ2686 状态压缩
- 如何选择 compileSdkVersion, minSdkVersion 和 targetSdkVersion
- Asp.net文本框输入提示功能(jquery-autocomplete)
- cordova环境配置
- 网络编程随笔
- UDP打洞原理
- JavaScript基础学习笔记