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

[CareerCup] 【第三轮】7.7-7.13 CareerCup 4.5

全局:

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

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

x
4.5 Implement a function to check if a binary tree is a binary search tree.

回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------Optional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】


Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎讨论,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改。




上一篇:【第三轮】7.7-7.13 CareerCup 4.4
下一篇:【第三轮】7.7-7.13 CareerCup 4.6
推荐
grassgigi 2014-7-8 10:16:07 | 只看该作者
全局:
本帖最后由 grassgigi 于 2014-7-8 10:42 编辑

【解题思路】
BFS - perform a inorder traversing, keep tracking two consecutive visited nodes, if the current one's value is smaller than the previous one, return false since BST is monotonic increasing inorder.

【时间复杂度】
O(N)

【空间复杂度】
O(N)

【gist link】
https://gist.github.com/chrislukkk/24270f55880fa1060d53
binray tree & tree builder:
https://gist.github.com/chrislukkk/2a1f825b8e924ea773e8

点评

Good job. I never thought use stack to implement BST...usually I use a queue...  发表于 2014-7-13 08:30
回复

使用道具 举报

全局:
【解题思路】
1. left.data < curr.data < right.data, recursively check left and right child (WRONG!!! NOT SUFFICIENT CONDITION!!!)
2. in-order traversal, keep track with the previous value and compare to the current one
3. check whether ALL left nodes are less than or equal to the current node, which must be less than ALL the right nodes
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist link】
https://gist.github.com/bcb725181e5a8f6035f3
【test case】
build binary tree using function developed in 4.3
array = [5 10 25 20 25 30 35]
Expected output: 0 (false)
Approach 1: 1
Approach 2: 0
Approach 3: 0
回复

使用道具 举报

全局:
【解题思路】
recursion: check from root to leaves, passing the left & right bound to every sub-call

【时间复杂度】
O(n)

【空间复杂度】
O(logn)

【gist link】
https://gist.github.com/happyWinner/7023673688565a1f402

评分

参与人数 1大米 +3 收起 理由
kimiflasky + 3 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
donnice 2014-7-12 02:58:43 | 只看该作者
全局:
【解题思路】
遍历每个点,保证右支的点都大,左支的点都小
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist】
https://github.com/donnice/donnice/blob/master/Q4_5
回复

使用道具 举报

🔗
bitcpf 2014-7-13 08:28:48 | 只看该作者
全局:
【解题思路】BST each node, compare left to current, current to right. Ref to 2nd codes
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist】https://gist.github.com/bitcpf/2d94f8556e160d311d96
回复

使用道具 举报

🔗
兰橘清檬 2014-7-13 09:29:07 | 只看该作者
全局:
【解题思路】
recursion with min and max limitation
【时间复杂度】
O(N)
【空间复杂度】
O(log N)
【gist】
https://gist.github.com/JoyceeLee/afb2727dc92838e881be
回复

使用道具 举报

🔗
Neal 2014-7-13 10:30:10 | 只看该作者
全局:
【解题思路】Inorder transverse the tree, check predecessor is smaller than current node
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist】https://gist.github.com/nealhu/9b8c4f9e306e61c3f0bd
回复

使用道具 举报

🔗
wilbert 2014-7-14 06:08:43 | 只看该作者
全局:
【解题思路】
recursively traverse the binary tree, compare the node with min and max value
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist】
https://gist.github.com/iwilbert/eb5b9c51613be3224aba
回复

使用道具 举报

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

本版积分规则

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