摘要: Q:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.给定一个递增排序好的链表,将其转换成一个平衡的二叉查找树。A: 这道题与“Convert Sorted Array to BST” 不同。数组随即访问的效率是O(1),所以可以快速的找到中间元素,而链表随即访问的效率为O(n),因此不同用之前的方法。Top-down的方法不能用了,改用:bottom-up的方式建立BST。随着list的遍历,创建树的结点,避免了链表的随机访问 阅读全文
posted @ 2013-06-04 15:45 summer_zhou 阅读(123) 评论(0) 推荐(0) 编辑
摘要: Q:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1.判断一个二叉树是否高度平衡:对任意结点,其左右子树的高度差<=1.A: 递归的对每个结点做判断是否平衡。子树的height<0,表示该子树不平衡。 bool is 阅读全文
posted @ 2013-06-04 14:44 summer_zhou 阅读(120) 评论(0) 推荐(0) 编辑
摘要: Q: Given a binary tree, return thebottom-up level ordertraversal 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 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],]按... 阅读全文
posted @ 2013-06-04 10:24 summer_zhou 阅读(114) 评论(0) 推荐(0) 编辑