![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
递归
文章平均质量分 77
likecool21
这个作者很懒,什么都没留下…
展开
-
LeetCode刷题笔录 Pow(x,n)
Implement pow(x, n). z原创 2014-04-24 13:24:20 · 690 阅读 · 0 评论 -
LeetCode刷题笔录Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree.原创 2014-08-19 10:58:50 · 706 阅读 · 0 评论 -
LeetCode刷题笔录Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devis原创 2014-08-20 00:08:37 · 674 阅读 · 0 评论 -
LeetCode刷题笔录N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions.原创 2014-08-21 04:09:31 · 543 阅读 · 0 评论 -
LeetCode刷题笔录Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].原创 2014-08-21 08:55:55 · 588 阅读 · 0 评论 -
LeetCode刷题笔录 Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary原创 2014-04-24 14:14:41 · 573 阅读 · 0 评论 -
LeetCode刷题笔录Word Search
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, where "adjacent" cells are those horizontally or vertically原创 2014-08-13 08:15:06 · 548 阅读 · 0 评论 -
LeetCode刷题笔录Covert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 思路并不难。每次选链表的中间元素原创 2014-08-01 12:47:11 · 611 阅读 · 0 评论 -
LeetCode刷题笔录Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "原创 2014-11-07 16:18:23 · 825 阅读 · 0 评论 -
LeetCode刷题笔录Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","原创 2014-10-04 00:45:14 · 577 阅读 · 0 评论 -
LeetCode刷题笔录Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1原创 2014-10-15 06:02:49 · 558 阅读 · 0 评论 -
LeetCode刷题笔录Subsets
自己总是写不出递归的方法,只能用f原创 2014-07-21 08:10:50 · 1040 阅读 · 0 评论 -
LeetCode刷题笔录Binary Tree Maximum Path Sum
这题试了很多次也没成功,从网上找了段d原创 2014-11-05 12:47:41 · 658 阅读 · 0 评论 -
LeetCode刷题笔录Sort List
Sort a linked list in O(n log n) time using constant space complexity. 挺有意思的题,这里用了merge sort,可以利用到merge sorted list 那题的代码 /** * Definition for singly-linked list. * class ListNode { *原创 2014-11-29 13:18:48 · 610 阅读 · 0 评论 -
LeetCode刷题笔录Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota原创 2014-07-16 05:23:59 · 784 阅读 · 0 评论 -
LeetCode刷题笔录Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit st原创 2014-08-05 04:45:57 · 1063 阅读 · 0 评论 -
LeetCode刷题笔录 Maximum Depth of Binary Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 递归吧。原创 2014-04-27 06:53:47 · 686 阅读 · 0 评论 -
LeetCode刷题笔录 Permutaions
public class Solution { public ArrayList> permute(int[] num) { ArrayList> result = new ArrayList>(); //start from an empty list result.add(new ArrayList()); for (转载 2014-04-12 02:42:04 · 762 阅读 · 0 评论 -
LeetCode刷题笔录 Same Tree
public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null && q == null) return true; else if(p == null || q == null) return f原创 2014-04-27 12:46:10 · 700 阅读 · 0 评论 -
LeetCode刷题笔录Unique Binary Search Trees
这里有个讲解(印度人口音真难懂)原创 2014-04-27 13:33:30 · 938 阅读 · 0 评论 -
LeetCode刷题笔录Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f原创 2014-06-25 13:09:24 · 769 阅读 · 0 评论 -
LeetCode刷题笔录Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1原创 2014-07-21 05:54:37 · 643 阅读 · 0 评论 -
LeetCode刷题笔录Convert Sorted Array to Balanced Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这题可以kan'chu'lai原创 2014-05-29 06:01:16 · 783 阅读 · 0 评论 -
LeetCode刷题笔录Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited numb原创 2014-08-02 04:02:49 · 594 阅读 · 0 评论 -
LeetCode刷题笔录Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \原创 2014-08-02 00:24:08 · 767 阅读 · 0 评论 -
LeetCode刷题笔录Sudoku Solver
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 only one unique solution. A sudoku puzzle.原创 2014-08-05 08:21:53 · 590 阅读 · 0 评论 -
LeetCode刷题笔录N-Queens
这题在CTCIsh原创 2014-07-29 02:53:40 · 608 阅读 · 0 评论 -
LeetCode刷题笔录Combinations
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: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]原创 2014-07-15 12:20:18 · 687 阅读 · 0 评论 -
LeetCode刷题笔录Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3原创 2014-08-05 04:03:38 · 897 阅读 · 0 评论 -
Largest BST in a Binary Tree
一到面试题,挺有意思的。 简单做法是对于每个node都进行一遍isBST的测试,找出最大的那个。 public static int largestBSTNaive(TreeNode root){ if(isBST(root)){ return size(root); } return Math.max(largestBSTNaive(root.left), largestB原创 2015-03-05 12:11:59 · 1849 阅读 · 0 评论