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

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

全局:

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

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

x
Given a sorted (increasing order) array, write an algorithmto create a binary search tree with minimal height.



上一篇:[第二轮] 3/11-3/17 CareerCup 4.2
下一篇:[第二轮] 3/11-3/17 CareerCup 4.4
🔗
zuohr 2013-3-10 23:19:58 | 只看该作者
全局:
Divide and conquer. In pseudo code :
buildtree:
if(array length is positive) {
add the middle node of the array to the tree;
buildtree from the array left to the middle point;
buildtree from the array right to the middle point;
}

In constructor(int[]) and buildTree()
https://gist.github.com/Zuohr/5128929
回复

使用道具 举报

🔗
EchoMemory 2013-3-11 16:53:37 | 只看该作者
全局:
本帖最后由 EchoMemory 于 2013-3-11 16:55 编辑

Node* build(ll,rr)
{
if check(ll,rr) return null;
int m=(ll+rr)/2;
node * a = new node(array[m]);
a->left=build(ll,m-1);
a->right=build(m+1,rr);
return a;
}
Node *root=build(...)
回复

使用道具 举报

🔗
sing1ee 2013-3-11 23:36:56 | 只看该作者
全局:
I choose the the middle value as the root and recur

my gist:https://gist.github.com/sing1ee/5135093
回复

使用道具 举报

🔗
moophis 2013-3-13 21:18:53 | 只看该作者
全局:
Recursively divide array by two to find the root of each sub trees.
https://github.com/moophis/careercup/blob/master/4.3.cpp
回复

使用道具 举报

🔗
grassgigi 2013-3-16 08:29:46 | 只看该作者
全局:
递归建立sub-tree,keep choosing middle value as the root in sub-tree
https://gist.github.com/chrislukkk/5174280
回复

使用道具 举报

全局:
*递归的认识还是不到位,对这种思维还不习惯,希望通过大量做题能够解决这个问题
*通过递归每次取中间的那个,形式上和快排有点类似。。。。

https://github.com/1094401996/Ca ... /MinHeightTree.java
回复

使用道具 举报

🔗
ThunderXu 2013-3-16 16:51:51 | 只看该作者
全局:
https://gist.github.com/ThunderXu/5175566
use recursion, take the middle element to be the root and recursively generate left sub-tree using left side of array and generate right sub-tree using right side of array.
回复

使用道具 举报

🔗
weep_willow 2013-3-17 12:22:16 | 只看该作者
全局:
recursively find the middle value of the array, and put the value as the root of the subtree

c++ implementation- function 4_3

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

使用道具 举报

🔗
sing1ee 2013-3-17 23:07:19 | 只看该作者
全局:
规格严格理性帝 发表于 2013-3-16 10:17
*递归的认识还是不到位,对这种思维还不习惯,希望通过大量做题能够解决这个问题
*通过递归每次取中间的那 ...

是要发这个吧。https://github.com/1094401996/Ca ... ree/BinaryTree.java
回复

使用道具 举报

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

本版积分规则

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