摘要: Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation ofs1="great": great / \ gr eat / \ / \g r e at / \ a tTo scramble the string, we may choose any non-leaf no... 阅读全文
posted @ 2013-08-23 23:19 feiling 阅读(293) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:[ [3], ... 阅读全文
posted @ 2013-08-23 22:10 feiling 阅读(244) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6[解题思路] 1 \ ... 阅读全文
posted @ 2013-08-23 21:14 feiling 阅读(1232) 评论(0) 推荐(1) 编辑
摘要: Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(k) extra space?[解题思路]从后往前计算,另外Trangle的index从0开始,故分配的数组大小为rowIndex + 1 1 public class Solution { 2 public ArrayList getRow(int rowIndex) { 3 ... 阅读全文
posted @ 2013-08-23 12:59 feiling 阅读(304) 评论(0) 推荐(0) 编辑
摘要: GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]][解题思路]第三行开始,每行除边界元素外,每个元素ai都是由ai-1 + ai构成 1 public class Solution { 2 public ArrayList> generate(int numRows) { 3 // Start typing your Ja... 阅读全文
posted @ 2013-08-23 11:18 feiling 阅读(433) 评论(0) 推荐(0) 编辑
摘要: Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.[解题思路]与上题类似Construct Binary Tree from Preorder and Inorder Traversal确定左右子树的序列,递归建立树,唯一不同在于后序遍历,根节点在最后一个元素 1 /** 2 * Definition for binary tree 3 * public class Tr.. 阅读全文
posted @ 2013-08-23 10:53 feiling 阅读(308) 评论(0) 推荐(0) 编辑
摘要: Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.[解题思路]由前序遍历知第一个节点是根节点,根据此节点值去中序遍历集合中找该root值所处位置,该位置之前的数属于左子树,之后的属于右子树,即找到左子树和右子树所处的子序列,接下来就可以用递归来完成递归的终止条件:preorder中仅有一个元素且preorder中的元素和inorder中元素相同 1 /** 2 * @auth. 阅读全文
posted @ 2013-08-23 10:33 feiling 阅读(446) 评论(0) 推荐(0) 编辑