摘要: Given a binary tree, return the preorder traversal of its nodes' values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is trivial, could you do it iteratively?思考:不可以递归,那么用栈存储结点。先用递归法做,算是加深记忆。需要注意ret需要清空,不然会记录上一次测试用例,所以将先序遍历单独写出来。class Solutio... 阅读全文
posted @ 2013-11-14 22:04 七年之后 阅读(147) 评论(0) 推荐(0) 编辑
摘要: Sort a linked list using insertion sort.思考:画图帮助理解,考虑以下情况:空链表,单结点链表,两个结点(第二个结点大于或小于第一个),多个结点。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *insertionSortList(Li... 阅读全文
posted @ 2013-11-14 17:17 七年之后 阅读(267) 评论(0) 推荐(0) 编辑
摘要: Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.思考:复习KMP算法。class Solution {public: char *strStr(char *haystack, char *needle) { // IMPORTANT: Please reset any member data you declared, as // the same Solut... 阅读全文
posted @ 2013-11-14 13:41 七年之后 阅读(171) 评论(0) 推荐(0) 编辑