https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Mock%20Interviews/Ride%20Share%20Start-Up%20Company/Ride%20Share%20Company%20-%20Interview%20Questions%20-%20SOLUTIONS/On-Site%20Question%203%20-%20SOLUTION.ipynb

On-Site Question 3 - SOLUTION

Problem

Given a binary tree, check whether it’s a binary search tree or not.

 

Requirements

Use paper/pencil, do not code this in an IDE until you've done it manually

Do not use built-in Python libraries to do this, but do mention them if you know about them

 

Solution

The first solution that comes to mind is, at every node check whether its value is larger than or equal to its left child and smaller than or equal to its right child (assuming equals can appear at either left or right). However, this approach is erroneous because it doesn’t check whether a node violates any condition with its grandparent or any of its ancestors.

So, we should keep track of the minimum and maximum values a node can take. And at each node we will check whether its value is between the min and max values it’s allowed to take. The root can take any value between negative infinity and positive infinity. At any node, its left child should be smaller than or equal than its own value, and similarly the right child should be larger than or equal to. So during recursion, we send the current value as the new max to our left child and send the min as it is without changing. And to the right child, we send the current value as the new min and send the max without changing.

 
 
class Node:
def __init__(self, val=None):
self.left, self.right, self.val = None, None, val INFINITY = float("infinity")
NEG_INFINITY = float("-infinity") def isBST(tree, minVal=NEG_INFINITY, maxVal=INFINITY):
if tree is None:
return True
if not minVal <= tree.val <= maxVal:
return False return isBST(tree.left, minVal, tree.val) and isBST(tree.right, tree.val, maxVal)
 
 

There’s an equally good alternative solution. If a tree is a binary search tree, then traversing the tree inorder should lead to sorted order of the values in the tree. So, we can perform an inorder traversal and check whether the node values are sorted or not.

 
 
def isBST2(tree, lastNode=[NEG_INFINITY]): 

    if tree is None:
return True if not isBST2(tree.left, lastNode):
return False if tree.val < lastNode[0]:
return False lastNode[0]=tree.val return isBST2(tree.right, lastNode)
 

This is a common interview problem, its relatively simple, but not trivial and shows that someone has a knowledge of binary search trees and tree traversals.

Good Job!

最新文章

  1. Java Web项目RSA加密
  2. .htaccess下Flags速查表
  3. Android,visibility属性
  4. YTU 2019: 鞍点计算
  5. 15stl模板
  6. Mac Pro更换SSD后,在Win7下启用ACHI的方法AHCI
  7. oracle Form Builer:FIND_FORM Built-in
  8. 你好,C++(22) 排排坐,吃果果——4.3.3 for循环:某个范围内…每个都…
  9. 文本框脚本 - select 事件
  10. 二十六、Jcreator使用初步
  11. Nginx之http_image_filter_module模块使用
  12. C字符串处理函数
  13. [编程笔记]第二章 C语言预备知识
  14. 为Druid监控配置访问权限(配置访问监控信息的用户与密码)
  15. oracle中查找某用户执行某张表的操作操作记录
  16. sench touch 自定义小图标(转)
  17. 关于css3中的flex
  18. Visual Studio 2012自动添加注释(如版权信息等)
  19. JavaScript的DOM_操作行内样式
  20. 属性attribute和property的区别

热门文章

  1. ASP.NET CMS: Administration Template
  2. leetcode13
  3. PHP 实现-多线程编程
  4. Can not find the tag library descriptor for &quot;http://java.sun.com/jsp/jstl/co
  5. Utils使用
  6. c++实现一个比较两个string类型的版本号的小demo
  7. C++析构函数的自动调用问题
  8. 复习:使用HTML编写简单程序
  9. 类的&quot;魔法&quot;方法
  10. 疯狂JAVA——第六章 面向对象(下)