题目
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
方法
数组是有序的,要求创建的二叉树尽量平衡,很容易想到对数组进行二分操作,左边的数组元素是左子树,右边的数组元素是右子树。进行递归操作就可以了。
TreeNode...
分类:
其他好文 时间:
2014-06-20 11:06:46
阅读次数:
257
【题目】
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
【题意】
给定一个已排序的数组(不存在重复元素),将它转换成一棵平衡二叉搜索树。
【思路】
由于平衡二叉树要求左右子树的高度差绝对值相遇等于1,也就是说左右子树尽可能包含相同数目节点。
则使用二分法来解本题即可。...
分类:
其他好文 时间:
2014-06-20 11:03:31
阅读次数:
246
sing names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the
alphabetical v...
分类:
其他好文 时间:
2014-06-20 10:37:22
阅读次数:
230
题目
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
方法
和有序数组的思想基本一样,将链表进行二分。
TreeNode getBST(ListNode head, int len) {
i...
分类:
其他好文 时间:
2014-06-20 09:46:33
阅读次数:
267
【题目】
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7},
3
/ 9 20
/ 15 7
return its b...
分类:
其他好文 时间:
2014-06-20 09:42:30
阅读次数:
224
Given an array where elements are sorted in
ascending order, convert it to a height balanced BST./** * Definition for binary
tree * public class TreeN...
分类:
其他好文 时间:
2014-06-11 21:58:10
阅读次数:
300
Given a singly linked list where elements are
sorted in ascending order, convert it to a height balanced BST.public class
Solution { /** Convert th...
分类:
其他好文 时间:
2014-06-10 00:22:44
阅读次数:
259
Given an array withnobjects colored red, white
or blue, sort them so that objects of the same color are adjacent, with the
colors in the order red, wh...
分类:
其他好文 时间:
2014-06-07 20:11:53
阅读次数:
234
【题目】
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
【题意】
将一个有序链表转换成平衡二叉树
【思路】
思路跟Convert Sorted Array to Binary Search Tree完全一样...
分类:
其他好文 时间:
2014-06-07 16:20:05
阅读次数:
287
Given an array withnobjects colored red, white
or blue, sort them so that objects of the same color are adjacent, with the
colors in the order red, wh...
分类:
其他好文 时间:
2014-06-05 17:49:43
阅读次数:
304