严氏北美IT公司面试真题汇总和解答论坛
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Validate BinarySearchTree

Go down

Validate BinarySearchTree Empty Validate BinarySearchTree

Post by Admin Sat Oct 21, 2017 4:08 pm

public boolean isValidBST(BTreeNode root) {
if(root==null)
return true;

return recurse(root, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
}

public boolean recurse(BTreeNode root, double low, double high){

if(root.val<=low || root.val>=high)
return false;

if(root.left!=null && !recurse(root.left, low, root.val))
return false;

if(root.right!=null && !recurse(root.right, root.val, high))
return false;

return true;
}

Admin
Admin

Posts : 124
Join date : 2017-10-21

https://csinterviewquestions.forumotion.com

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum