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

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

全局:

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

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

x
4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.

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


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




上一篇:【第三轮】7.7-7.13 CareerCup 3.7
下一篇:【第三轮】7.7-7.13 CareerCup 4.2
推荐
chouclee 2014-7-7 11:26:03 | 只看该作者
全局:
本帖最后由 chouclee 于 2014-7-7 11:30 编辑

【解题思路】
递归遍历每一个点,计算左右两个subtree的高度差
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist link】
https://gist.github.com/chouclee/dac2df817f826f5df3a4
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】    /*                            7
                         *                         /    \
                         *                     /         \
                         *                  4            9
                         *                 / \         / \
                         *              2   5     8   10
                         *            / \    \            \
                         *         1   3    6          11
                         *        /
                        *       0
                 */  




补充内容 (2014-7-9 12:54):
gist link贴错了。。。囧 https://gist.github.com/chouclee/5a7bdbed1ab1c8b3d150

点评

我觉得这个test case的输出应该是balanced,每个node的height差都没有超过1。。。  发表于 2014-7-9 00:13
回复

使用道具 举报

推荐
donnice 2014-7-7 07:39:46 | 只看该作者
全局:
【解题思路】
参考了网上的思路,用递归遍历每一个点,并计算其左右树的高度差。凡是有超过1的就说明不平衡
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist link】
https://github.com/donnice/donnice/blob/master/Q4_1
【Test】
例子里我用的树分别是:
              5                                                                7
            2                  8                                            2                     8
      1          4       7                                          1             4      
            3                                                                3            5
回复

使用道具 举报

🔗
grassgigi 2014-7-6 23:35:04 | 只看该作者
全局:
【解题思路】
Recursion.

自己写的时候return了一个boolean值(表示subtree是否balance)和int值(height of the subtree)
书上的答案return -1如果subtree is not balance, 省下了boolean的空间

【时间复杂度】
O(N) - N is # of tree nodes

【空间复杂度】
O(N)

【gist link】
https://gist.github.com/chrislukkk/6581e137e1ad9673a973
BinaryTree: https://gist.github.com/chrislukkk/2a1f825b8e924ea773e8

【test case】
A binary tree builder: Building a tree based on itspreorder and inorder sequence
https://gist.github.com/chrislukkk/2a1f825b8e924ea773e8
回复

使用道具 举报

全局:
【解题思路】
参考书后解答=。=
【时间复杂度】
O(N)
【空间复杂度】
O(H)
【gist link】
https://gist.github.com/e448a924ba7d3b4d0f8b
【test case】
BST:
5 3 4 1 2 6  -> false
5 3 4 1 2 6 8 -> true
5 3 4 1 2 6 8 1 -> false
回复

使用道具 举报

🔗
bitcpf 2014-7-9 00:12:13 | 只看该作者
全局:
【解题思路】Ref to the book... count the left height and right height of each node, also copy the tree structure from lss
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist link】
https://gist.github.com/bitcpf/26394ee53639cdbf721f
回复

使用道具 举报

🔗
jason51122 2014-7-9 14:26:07 | 只看该作者
全局:
【解题思路】Return the current height for helper() function. Return -1 if the tree is not balanced.
【时间复杂度】O(N)
【空间复杂度】O(H)
【gist link】https://gist.github.com/jason51122/ec57311f8d1fcd8e9e41
回复

使用道具 举报

🔗
林微熙 2014-7-10 05:45:56 | 只看该作者
全局:
【解题思路】recurse through the entire tree, compute the height of each subtree
【时间复杂度】o(n^2)
【空间复杂度】o(n)
【gist link】https://gist.github.com/hilda8519/24a6435951a05ac1b8de

答案的第二个例子不错
回复

使用道具 举报

🔗
Neal 2014-7-10 07:32:14 | 只看该作者
全局:
【解题思路】transverse the tree recursively, examine each node and check if two subtrees are balanced.
【时间复杂度】o(n)
【空间复杂度】o(max(height))
【gist link】https://gist.github.com/nealhu/091a742d6c11d5298760
回复

使用道具 举报

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

本版积分规则

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