查看: 2273| 回复: 10
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] [第二轮] 3/11-3/17 CareerCup 4.5

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Implement a function to check if a binary tree is a binary searchtree.



上一篇:[第二轮] 3/11-3/17 CareerCup 4.4
下一篇:[第二轮] 3/11-3/17 CareerCup 4.6
🔗
EchoMemory 2013-3-11 16:19:18 | 只看该作者
全局:
本帖最后由 EchoMemory 于 2013-3-16 14:51 编辑

initial: flag=1
when traversing a binary tree, if the value of left node is greater than the parent node or the value of right is smaller than the parent node, flag=0; break the recursion. Here, 'parent' means all parents nodes...  My poor english...
回复

使用道具 举报

🔗
sing1ee 2013-3-11 23:36:14 | 只看该作者
全局:
回复

使用道具 举报

🔗
moophis 2013-3-15 19:31:42 | 只看该作者
全局:
Recursively check each node with its children if it has any, while updating the upper bound and lower bound of possible value its children in a BST should have.
https://github.com/moophis/careercup/blob/master/4.5.cpp

回复

使用道具 举报

🔗
moophis 2013-3-15 19:33:17 | 只看该作者
全局:
EchoMemory 发表于 2013-3-11 16:19
initial: flag=1
when traversing a binary tree, if the value of left node is greater than the parent ...

It is not the whole story. You should consider more scenarios.
回复

使用道具 举报

🔗
grassgigi 2013-3-16 10:08:03 | 只看该作者
全局:
first method: recursive, keep tracking min and max value when traverse down the sub tree
                       Time O(n), space O(1);

second method: in order traverse the tree and keep tracking previous node and current node
                       Time O(n), space O(n);

https://gist.github.com/chrislukkk/5174594
回复

使用道具 举报

🔗
EchoMemory 2013-3-16 14:49:33 | 只看该作者
全局:
本帖最后由 EchoMemory 于 2013-3-16 14:52 编辑
moophis 发表于 2013-3-15 19:33
It is not the whole story. You should consider more scenarios.

I knew what you mean. Thx for reminder, I edited it again. Sorry for adding confusion, I described the solution too brief and unclear...
回复

使用道具 举报

🔗
ThunderXu 2013-3-16 19:40:56 | 只看该作者
全局:
https://gist.github.com/ThunderXu/5176045
use recursion, for each node compare it with the max value of its left sub-tree and the min value of its right sub-tree
回复

使用道具 举报

🔗
weep_willow 2013-3-17 12:20:43 | 只看该作者
全局:
recursively check whether the left<middle<right value holds at each node

c++ implementation, see function bool isBST()

https://github.com/StanleyLee/CareerCup/blob/master/4_1.cpp
回复

使用道具 举报

全局:
https://github.com/1094401996/Ca ... fourdot5/IsBST.java

好吧,自己写出递归的感觉还是不错的。。。。不过似乎方法恶心了点。。。下周再优化一下。。。

每次感觉顶不下去的时候就看看地里编程算法面试题板块越来越少的跟帖。。。。。那么多人已经放弃了,,,,我还没有。。。有点小阴暗 继续坚持下去。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表