摘要: 给出一棵BST,将其转换为双向链表,left保存前节点,right保存后节点。例如: 4 / \ 1 7 / \ / \ 0 2 5 8 \ \ \ 3 6 9变为:0<->1<->2<->3<->4<->5<->6<->7<->8<->9.解答:这里我们可以看到一个节点的左子树的最后节点就是本节点的前驱,右子树的最左节点就是后继。于是需要一个函数能够返回这样的需求,也就有了一个可以返回本棵树的左右节点的函数。同时,在细节上进行一些处理。 1 #include <iostre... 阅读全文
posted @ 2012-10-29 20:14 chkkch 阅读(599) 评论(0) 推荐(1) 编辑
摘要: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak) must 阅读全文
posted @ 2012-10-29 17:36 chkkch 阅读(4453) 评论(0) 推荐(0) 编辑
摘要: Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak 阅读全文
posted @ 2012-10-29 17:20 chkkch 阅读(4181) 评论(0) 推荐(0) 编辑
摘要: Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.Note: You 阅读全文
posted @ 2012-10-29 16:57 chkkch 阅读(6461) 评论(0) 推荐(2) 编辑
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.这题的关键是能找出当前链表的中间节点,然后再递归左右的子链表,开始的时候程序先计算链表总厂,然后传入两个前后索引指针,最后每次递归找出中间节点即可。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 *... 阅读全文
posted @ 2012-10-29 15:20 chkkch 阅读(4022) 评论(2) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.二分递归转换 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 *... 阅读全文
posted @ 2012-10-29 15:08 chkkch 阅读(2387) 评论(0) 推荐(0) 编辑
摘要: Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.树的递归 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val... 阅读全文
posted @ 2012-10-29 14:57 chkkch 阅读(1148) 评论(0) 推荐(0) 编辑
摘要: Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.递归构造 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : va... 阅读全文
posted @ 2012-10-29 14:49 chkkch 阅读(3240) 评论(0) 推荐(0) 编辑
摘要: Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]简单的组合问题,用类似dfs的方法 1 class Solution { 2 private: 3 vector<vector<int> > ret; 4 vector<int> a; 5 public: 6 void sol 阅读全文
posted @ 2012-10-29 14:38 chkkch 阅读(4079) 评论(0) 推荐(0) 编辑
摘要: You are climbing a stair case. It takesnsteps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?fib数列 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT ... 阅读全文
posted @ 2012-10-29 14:22 chkkch 阅读(637) 评论(0) 推荐(0) 编辑