摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Solution:BinaryTree* sortedArrayToBST(int arr[], int start, int end) { if (start > end) return NULL; // same as (start+end)/2, avoids overflow. int mid = start + (end - start) / 2; BinaryTree *node =. 阅读全文
posted @ 2014-03-23 17:35 andyqee 阅读(94) 评论(0) 推荐(0) 编辑
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Solution:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *//** * Definition for binary tre... 阅读全文
posted @ 2014-03-23 17:20 andyqee 阅读(125) 评论(0) 推荐(0) 编辑
摘要: 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 of every node never differ by more than 1.Solution:/** * Definition for binary tree * struct TreeNode { * int val; * Tree... 阅读全文
posted @ 2014-03-23 15:14 andyqee 阅读(96) 评论(0) 推荐(0) 编辑