题意:给定一棵二叉树,返回按zigzag层次遍历的结果
思路:
还是跟前面的Binary Tree Level Order Traversal的思路一样
即从上往下按层遍历二叉树,将每一层的节点存放到该层对应的数组中
最后将得到的总数组中奇数层(从0层开始计数)的子数组reverse一下就可以了
复杂度:时间O(n),空间O(n)...
分类:
其他好文 时间:
2014-05-15 15:15:50
阅读次数:
374
题意:给定一棵二叉树,返回按层遍历的结果
思路1:bfs,定义一个新的struct,记录指针向节点的指针和每个节点所在的层
复杂度1:时间O(n),空间O(n)
思路2:dfs
递归函数:
void levelOrder(TreeNode *root, int level, vector<vector >&result)
表示把根为root的树按层存放在result中,其中level表示当前的层数
复杂度2:时间O(n),空间O(n)
相关题目:...
分类:
其他好文 时间:
2014-05-15 14:46:06
阅读次数:
355
Determine whether an integer is a palindrome. Do this without extra space....
分类:
其他好文 时间:
2014-05-15 14:38:59
阅读次数:
278
Given an array of integers, every element appears twice except for one. Find that single one....
分类:
其他好文 时间:
2014-05-15 07:20:39
阅读次数:
264
题意:从底往上按层遍历二叉树
思路:
思路和Binary Tree Level Order Traveral 一样,
即从上往下按层遍历二叉树,将每一层的节点存放到该层对应的数组中
最后将得到的数组倒转一下就可以了
按层遍历二叉树可用bfs,也可用dfs,但都要记录节点所在的层
复杂度:时间O(n), 空间O(n)...
分类:
其他好文 时间:
2014-05-15 06:32:14
阅读次数:
278
【题目】
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Out...
分类:
其他好文 时间:
2014-05-15 05:13:49
阅读次数:
306
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specifi...
分类:
其他好文 时间:
2014-05-15 05:00:50
阅读次数:
229
又好又难的题。
我发现难题一般都包含了好几个简单题,必须对一些基本的算法熟练掌握才能快速准确的写出代码。这个题就用到了kmp,这个算法写过好多遍,还是不断的出错,哎。
题目中有两种符号,?可以匹配任意单个字符,*可以匹配任意0个或多个字符。?其实没什么,麻烦就麻烦在*上。一个*实际上代表了一段任意的字符串。很容易想到的一种解法是用递归,当p[i]不为*时,看p[i]是不是'?'或者p[i]==...
分类:
其他好文 时间:
2014-05-15 04:19:36
阅读次数:
265
【题目】
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
【题意】
题意是找出字符串S中最长回文子串,S最长为1000,保证有唯一解
【思路】
原字符串用特殊字符#间隔,如下所示:
#a...
分类:
其他好文 时间:
2014-05-15 03:31:25
阅读次数:
299
题目一:
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
[
...
分类:
其他好文 时间:
2014-05-14 15:10:01
阅读次数:
293