首页 > 代码库 > CC150 4.3
CC150 4.3
4.3 Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height.
[1, 2, 4, 5, 6]
Create a BT with min height. => balanced tree.
Node build(int array) { return build(array, 0, array.length - 1, null); } O(log n) private Node build(int[] array, int from, int to, Node father) { if (from > to) return null; int middle = (from + to) / 2; Node n = new Node(middle); n.father = father; n.left = build(array[], from, middle - 1, n); n.right = build(array[], middle + 1, to, n); return n; }
CC150 4.3
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。