随笔分类 -  Recursion

摘要: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 dept 阅读全文
posted @ 2016-07-08 04:05 北叶青藤 阅读(190) 评论(0) 推荐(0)
摘要:Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes. The lowest common ancestor is the node with larg 阅读全文
posted @ 2016-07-08 02:55 北叶青藤 阅读(238) 评论(0) 推荐(0)
摘要:Given inorder and postorder traversal of a tree, construct the binary tree. Notice You may assume that duplicates do not exist in the tree. Given inor 阅读全文
posted @ 2016-07-08 00:21 北叶青藤 阅读(220) 评论(0) 推荐(0)
摘要:The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all 阅读全文
posted @ 2016-07-06 07:06 北叶青藤 阅读(173) 评论(0) 推荐(0)
摘要:Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Example Given [1,2,3,4,5,6,7], return 4 / \ 2 6 / \ / 阅读全文
posted @ 2016-07-01 05:31 北叶青藤 阅读(230) 评论(0) 推荐(0)
摘要:Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell 阅读全文
posted @ 2016-06-29 03:13 北叶青藤 阅读(313) 评论(0) 推荐(0)
摘要:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off 阅读全文
posted @ 2015-01-10 08:31 北叶青藤 阅读(131) 评论(0) 推荐(0)
摘要:问题:产生n位元的所有格雷码。 格雷码(Gray Code)是一个数列集合,每个数使用二进位来表示,假设使用n位元来表示每个数字,任两个数之间只有一个位元值不同。 例如以下为3位元的格雷码: 000 001 011 010 110 111 101 100 。 如果要产生n位元的格雷码,那么格雷码的个 阅读全文
posted @ 2015-01-06 09:01 北叶青藤 阅读(307) 评论(0) 推荐(0)
摘要:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: 转载请注明出处: cnbl 阅读全文
posted @ 2015-01-06 08:53 北叶青藤 阅读(188) 评论(0) 推荐(0)
摘要:问题: 给你一个数字长度,比如是 n ,那么打印出所有长度为 n 的数字,但是相邻数字不能一样。假如n = 3,那么121是可以的,112是不行的。 分析: 首先,该数字的第一个位置可以是1-9任意值,然后第二个位置可以是1-9任意值,但是不能和前面一个值相等。对于第三个位置,道理是一样的,所以,这 阅读全文
posted @ 2015-01-04 09:36 北叶青藤 阅读(344) 评论(0) 推荐(0)
摘要:问题:给一个值 n , 返回所有 n 位整数,每个整数里的值是单调递增的。 比如,n = 3,那么长度为3的整数有 123, 124, 125, 134,。。。等。122, 222,这些是不符合条件的。 1 public class Solution { 2 3 public static... 阅读全文
posted @ 2015-01-04 09:32 北叶青藤 阅读(194) 评论(0) 推荐(0)
摘要:Given a digit string, return all possible letter combinations that the number could represent. Example Given "23" Return ["ad", "ae", "af", "bd", "be" 阅读全文
posted @ 2015-01-02 07:26 北叶青藤 阅读(192) 评论(0) 推荐(0)
摘要:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: " 阅读全文
posted @ 2015-01-02 07:16 北叶青藤 阅读(191) 评论(0) 推荐(0)
摘要:问题:输入一个正数n,输出所有和为 n 的连续正数序列。例如:输入15,由于1+2+3+4+5 = 4+5+6 = 7+8=15,所以输出3个连续序列1-5、4-6和7-8。 1 public class Solution { 2 public static void main(String... 阅读全文
posted @ 2015-01-02 06:58 北叶青藤 阅读(840) 评论(0) 推荐(0)
摘要:Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be 阅读全文
posted @ 2015-01-02 06:30 北叶青藤 阅读(193) 评论(0) 推荐(0)
摘要:问题:在8×8的国际象棋上摆放八个皇后,使其不能相互攻击,即任意两个皇后不得处在同一行、同一列或者同一对角斜线上。请求出总共有多少种摆法。下图为一种为符合条件的摆放。思路:因为棋盘的长宽和皇后的个数是一样的,那么,每一个皇后总是占据其中的一列(或者一行,我们这里假设皇后占据的是列,所以,第i个皇后总... 阅读全文
posted @ 2015-01-02 06:06 北叶青藤 阅读(212) 评论(0) 推荐(0)
摘要:问题:在一个n*m的迷宫里,每一个坐标点有两种可能: 0 或 1。0表示该位置允许通过,1表示该位置不允许通过。从坐标(0,0)点出发,找出所有通往出口(n-1, m-1) 的路径。如果我们用二维矩阵来表示,那么地图可以表示成:0 0 0 0 01 0 1 0 10 0 0 0 10 1 0 0 0... 阅读全文
posted @ 2015-01-02 05:12 北叶青藤 阅读(203) 评论(0) 推荐(0)
摘要:问题:给一个字符串,比如ABC, 把所有的排列,即:ABC, ACB, BAC, BCA, CAB, CBC 都找出来。 解题思路:对于一个n 位的字符串来讲,它是n-1位字符串的排列 加上 没有在 n -1 位字符串里 那个字符 的排列。 有点难理解,用例子说明:对于字符串ABC来讲,它所有的排列 阅读全文
posted @ 2015-01-01 08:33 北叶青藤 阅读(368) 评论(0) 推荐(0)
摘要:Subsets I Given a set of distinct integers, return all possible subsets. Notice Elements in a subset must be in non-descending order. The solution set 阅读全文
posted @ 2014-12-31 11:24 北叶青藤 阅读(354) 评论(0) 推荐(0)