摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Solution: 1 TreeNode *sortedListToBST(ListNode *head) { 2 if(head == NULL) 3 return NULL; 4 if(head->next == NULL) 5 return new TreeNode(head->val)... 阅读全文
posted @ 2013-11-29 07:41 假日笛声 阅读(174) 评论(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 ofeverynode never differ by more than 1.Solution: 1 bool helperBalanced(int *depth, TreeNode * root){ 2 if(root->left... 阅读全文
posted @ 2013-11-29 07:22 假日笛声 阅读(184) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Summary: BFS or DFS with recursion. 1 int minDepth(TreeNode *root) { 2 if(root == NULL) 3 return 0; 4 if(root-... 阅读全文
posted @ 2013-11-29 06:34 假日笛声 阅读(168) 评论(0) 推荐(0) 编辑
摘要: Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that surrounded region .For example,X X X XX O O XX X O XX O X XAfter running your function, the board should be:X X X XX X X XX X X 阅读全文
posted @ 2013-11-29 06:17 假日笛声 阅读(1872) 评论(1) 推荐(0) 编辑
摘要: 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, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.Note:You are not suppose to use the l 阅读全文
posted @ 2013-11-29 05:16 假日笛声 阅读(237) 评论(0) 推荐(0) 编辑