摘要: Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.解法:递归求解,思路同上一篇博客中用先序和中序构造二叉树类似。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 ... 阅读全文
posted @ 2013-05-12 13:54 infinityu 阅读(200) 评论(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.解法:递归求解。 基本情况:先序与中序数组长度小于1时,返回NULL。 递归步骤:首先确定preorder的第一个元素一定是该树的root,再在inorder中找到该元素,标记为index,index左部为左子树的中序,右部为右子树的中序;随后通过左右子树中序的长度(可能为0),在preorder中确定左右子树的先序。 1 .. 阅读全文
posted @ 2013-05-12 13:38 infinityu 阅读(557) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return theinordertraversal of its nodes' values.Note:Recursive solution is trivial, could you do it iteratively?二叉树中序遍历,非递归解法。使用栈记录中序遍历时中间节点的访问顺序,从栈中弹出的顺序即为中序。Program Runtime:16 milli secs 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * Tr... 阅读全文
posted @ 2013-05-12 12:53 infinityu 阅读(152) 评论(0) 推荐(0) 编辑
摘要: Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.广度优先遍历,考察两棵树中每一个的值以及左右孩子的值是否相等。Program Runtime:8 milli secs 1 /** 2 * Definition for binary tree 3 * struct TreeNode.. 阅读全文
posted @ 2013-05-11 22:22 infinityu 阅读(170) 评论(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.分别考虑左右子树是否为空的情况并递归地进行计算。Program Runtime:60 milli secs 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * ... 阅读全文
posted @ 2013-05-11 21:57 infinityu 阅读(380) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1.递归地计算每个节点的左右子树深度,看其是否平衡。由于节点被重复访问,效率较低。Program Runtime:68 milli secs 1 /** 2 * Definitio. 阅读全文
posted @ 2013-05-11 21:21 infinityu 阅读(2866) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.广度优先遍历,使用NULL标记每层的结尾。Program Runtime:40 milli secs 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * Tre... 阅读全文
posted @ 2013-05-11 17:18 infinityu 阅读(618) 评论(0) 推荐(0) 编辑
摘要: 过去的一年多里,参加了一些面试,虽然面过的公司不多,但都从头一直走到尾。毕竟自己也是花了大量的时间和精力在这一场场的面试里。所以,就絮叨下自己的一些经验,希望能给在美国找实习找工作的同学们提供一点点帮助。开始前的一些说明:1. 笔者只是一介小本科,虽然留了学,但是留了级,学识浅薄,目光短浅,文章若有不恰之处,恳请各位大牛不吝指正!2. 笔者面试的岗位均为Software Engineer,俗称“程序猿”。如果读者是非CS专业或没有找此类工作的需求,请ctrl + w。本文更多的倾向于CS技术层面,关于面试仪表妆容礼仪等等的其他问题,请出门右拐。3. 鉴于保密协议,本文只谈面试准备材料和方法,不 阅读全文
posted @ 2013-04-17 10:55 infinityu 阅读(329) 评论(0) 推荐(0) 编辑
摘要: 单例模式,也叫单子模式,是一种常用的软件设计模式。在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。比如在某个服务器程序中,该服务器的配置信息存放在一个文件中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再通过这个单例对象获取这些配置信息。这种方式简化了在复杂环境下的配置管理。通常单例模式在Java语言中,有两种构建方式:饿汉方式。指全局的单例实例在类装载时构建。public class Singleton { private final static Singleton INSTANCE ... 阅读全文
posted @ 2013-04-14 14:13 infinityu 阅读(197) 评论(0) 推荐(0) 编辑
摘要: 题一、 给定单链表,检测是否有环。 使用两个指针p1,p2从链表头开始遍历,p1每次前进一步,p2每次前进两步。如果p2到达链表尾部,说明无环,否则p1、p2必然会在某个时刻相遇(p1==p2),从而检测到链表中有环。题二、 给定两个单链表(head1, head2),检测两个链表是否有交点,如果有返回第一个交点。 如果head1==head2,那么显然相交,直接返回head1。 否则,分别从head1,head2开始遍历两个链表获得其长度len1与len2。假设len1>=len2,那么指针p1由head1开始向后 移动len1-len2步。指针p2=head2,下面p1、p2每次向后 阅读全文
posted @ 2013-03-29 20:19 infinityu 阅读(268) 评论(0) 推荐(0) 编辑