上一页 1 ··· 6 7 8 9 10 11 12 下一页
摘要: Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].Note:Recursive solution is trivial, could you do it iteratively?解题思路:相比于前序遍历,后续遍历思维上难度要大些,前序遍历是通过一个stack,首先压入父亲结点,然后弹出父亲结点,并输出它的value,之后压人其右儿子,左儿子即可。然而后... 阅读全文
posted @ 2013-11-10 10:22 xchangcheng 阅读(3308) 评论(0) 推荐(0) 编辑
摘要: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0解题思路:顺序查找非常简单,这里尝试 阅读全文
posted @ 2013-11-09 22:35 xchangcheng 阅读(152) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return thepreordertraversal 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?解题思路:用迭代的方法实现二叉树的遍历,需要借助一些数据结构,比如list,stack等。首先在stack中push入当前的root,由于是前序遍历,故root的value是先于... 阅读全文
posted @ 2013-11-09 22:10 xchangcheng 阅读(1295) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.Initially, all next pointers are set toNULL.N... 阅读全文
posted @ 2013-11-09 19:36 xchangcheng 阅读(138) 评论(0) 推荐(0) 编辑
摘要: Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3.顺序扫描一遍即可,注意释放被删除的结点的空间。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; 阅读全文
posted @ 2013-11-09 13:19 xchangcheng 阅读(156) 评论(0) 推荐(0) 编辑
摘要: Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 ... 阅读全文
posted @ 2013-11-09 12:11 xchangcheng 阅读(466) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at 阅读全文
posted @ 2013-11-09 10:23 xchangcheng 阅读(143) 评论(0) 推荐(0) 编辑
摘要: Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!If the integer's last digit is 0, what should the output be? ie, cas 阅读全文
posted @ 2013-11-09 10:01 xchangcheng 阅读(381) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?关于这个问题可以参考http://leonax.net/p/1960/find-circle-in-linked-list/。由于每一个父亲只有可能有一个孩子,故这里的环实际上是指list中某一个节点的孩子同时也是它自己或者他的祖先。 这个问题需要注意几种情况:1. 空链表不成环2. 一个节点自环3. 一条链表完整成环不能开额外的空间,即空间复杂度是o(1)。该问题是经典面试问题, 阅读全文
posted @ 2013-11-08 17:32 xchangcheng 阅读(2520) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?感谢cnblog博主feiling,这篇博客一方面参考了feiling的博客,也加入了自己的一些看法。[解题思路]要求线性时间复杂度,同时空间复杂度为O(1),即只允许开常数个空间。最直接的思路是 阅读全文
posted @ 2013-11-08 07:43 xchangcheng 阅读(10928) 评论(18) 推荐(3) 编辑
上一页 1 ··· 6 7 8 9 10 11 12 下一页