随笔分类 -  算法

摘要:算法分析:求树的最小最大深度时候,都有两种方法,第一种是递归思想。树最大最小深度,即为它的子树的最大最小深度+1,是动态规划的思想。还有一种方法是层序遍历树,只不过求最小深度时,找到第一个叶子节点就可以返回,该节点的深度,即为树的最小深度。求最大深度时,需要层序遍历完整棵树。 阅读全文
posted @ 2016-09-27 02:04 32ddd 阅读(555) 评论(0) 推荐(0)
摘要:算法分析:递归和非递归两种方法。 阅读全文
posted @ 2016-09-27 01:26 32ddd 阅读(320) 评论(0) 推荐(0)
摘要:第一种也是最常用的一种,使用queue。还有一种不使用queue的方法。不使用queue的思路,其实就是每次都只存储一层的节点,然后遍历这一层的节点,是真正的按层遍历的思想。每次遍历的都是当前层,记录的都是当前层的下一层。 阅读全文
posted @ 2016-09-27 00:40 32ddd 阅读(9421) 评论(0) 推荐(1)
摘要:问题描述: 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 leve 阅读全文
posted @ 2016-09-26 20:10 32ddd 阅读(874) 评论(0) 推荐(0)
摘要:问题描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary t 阅读全文
posted @ 2016-09-26 17:47 32ddd 阅读(254) 评论(0) 推荐(0)
摘要:问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is 阅读全文
posted @ 2016-09-24 18:57 32ddd 阅读(214) 评论(0) 推荐(0)
摘要:算法分析:这道题很简单,利用递归即可。 阅读全文
posted @ 2016-09-03 16:34 32ddd 阅读(508) 评论(0) 推荐(0)
摘要:问题描述:题意就是二叉树中有两个节点交换了,恢复结构。 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. 算法分析:其 阅读全文
posted @ 2016-09-03 15:37 32ddd 阅读(311) 评论(0) 推荐(0)
摘要:算法分析:两种方法,一种是中序遍历,然后得到一个序列,看序列是否是有序的。第二种,是用递归。 中序遍历: 递归: 阅读全文
posted @ 2016-09-02 22:10 32ddd 阅读(221) 评论(0) 推荐(0)
摘要:问题描述: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac 阅读全文
posted @ 2016-09-02 20:23 32ddd 阅读(348) 评论(0) 推荐(0)
摘要:Unique Binary Search Trees:求生成二叉排序树的个数。 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n 阅读全文
posted @ 2016-08-31 19:26 32ddd 阅读(197) 评论(0) 推荐(0)
摘要:问题描述: Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", retu 阅读全文
posted @ 2016-08-31 16:30 32ddd 阅读(1437) 评论(1) 推荐(0)
摘要:ReverseLinkedList: ReverseLinkedList2: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NU 阅读全文
posted @ 2016-08-30 23:39 32ddd 阅读(680) 评论(0) 推荐(0)
摘要:问题描述: A message containing letters from A-Z is being encoded to numbers using the following mapping: Given an encoded message containing digits, deter 阅读全文
posted @ 2016-08-30 16:19 32ddd 阅读(709) 评论(0) 推荐(0)
摘要:问题描述: The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the tot 阅读全文
posted @ 2016-08-26 16:49 32ddd 阅读(528) 评论(0) 推荐(0)
摘要:问题描述: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible repres 阅读全文
posted @ 2016-08-26 15:55 32ddd 阅读(393) 评论(0) 推荐(0)
摘要:/* w代表物品重量,v代表物品价值,c代表背包最大能容纳重量 res[i][j]代表背包可以选择前i个物品,最大容量为j时候的最大价值 res[i-1][j] if(w[i-1] > j) res[i][j] = max((res[i-1][j-w[i-1]]+v[i-1]), res[i-1][j]) else **/ public c... 阅读全文
posted @ 2016-08-25 21:16 32ddd 阅读(217) 评论(0) 推荐(0)
摘要:问题描述: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserv 阅读全文
posted @ 2016-08-24 20:11 32ddd 阅读(233) 评论(0) 推荐(0)
摘要:问题描述: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. For example, given the fol 阅读全文
posted @ 2016-08-24 17:12 32ddd 阅读(367) 评论(0) 推荐(0)
摘要:问题描述: Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in th 阅读全文
posted @ 2016-08-24 16:15 32ddd 阅读(297) 评论(0) 推荐(0)