首页 > 其他 > 详细

Validate Binary Search Tree

时间:2019-02-22 16:09:04      阅读:143      评论:0      收藏:0      [点我收藏+]

题目:

Given a binary tree, determine if it is a valid Binary Search Tree (BST)

 

解答:

 1 public class Solution {
 2 
 3     public boolean isValidBST(TreeNode root) {
 4         return valid(root, null, null);
 5     }
 6 
 7     private boolean valid(TreeNode p, Integer low, Integer high) {
 8         if(p == null) {
 9             return true;
10         }
11 
12         return (low == null || p.val > low) && (high == null || p.val < high)
13                 && valid(p.left, low, p.val) && valid(p.right, p.val, high);
14     }   
15 
16 }

 

Validate Binary Search Tree

原文:https://www.cnblogs.com/wylwyl/p/10418718.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!