摘要: Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word does not exist, return 0.Note:A word is defined as a character sequence consists of non-space characters only.For example,Givens="Hello Worl 阅读全文
posted @ 2014-02-01 17:27 Razer.Lu 阅读(239) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which rep... 阅读全文
posted @ 2014-02-01 09:19 Razer.Lu 阅读(155) 评论(0) 推荐(0) 编辑
摘要: [解题思路]记录下一层元素个数,这样每次遍历时就知道何时结束,只需一个queue用一个count来计数每一层的node的个数,/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public ArrayList> levelOrder(TreeNode root) { ... 阅读全文
posted @ 2014-02-01 08:31 Razer.Lu 阅读(186) 评论(0) 推荐(0) 编辑
摘要: [解题思路]二叉树的层序遍历通过使用queue来实现,上网搜索了下,发现该题可以使用栈来解决,通过分析执行结果确实是后进先出这里通过定义leftToRight来表示是从左到右还是从右到左从左到右:先加left后加right从右到左:先加right后加leftorderlevel traversal 遍历最后每行添加到result前 判断是leftToRight 还是Right to Left/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * Tr... 阅读全文
posted @ 2014-02-01 08:05 Razer.Lu 阅读(182) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note:Recursive solution is trivial, could you do it iteratively?Recursive Version/** * Definition for binary tree * public class TreeNode { * int val;... 阅读全文
posted @ 2014-02-01 03:05 Razer.Lu 阅读(198) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { v... 阅读全文
posted @ 2014-02-01 02:08 Razer.Lu 阅读(141) 评论(0) 推荐(0) 编辑