摘要: 【试题描述】定义一个函数,给定二叉树,给每层生成一个链表We can do a simple level by level traversal of the tree, with a slight modification of the breath-first traversal of the treeIn a usual breath first search traversal, we simply traverse the nodes without caring which level we are on In this case, it is critical to know th 阅读全文
posted @ 2013-04-11 20:28 曾先森在努力 阅读(175) 评论(0) 推荐(0) 编辑
摘要: 【试题描述】定义一个函数,输入一个有序数组生成最小高度二叉树We will try to create a binary tree such that for each node, the number of nodes in the left subtree and the right subtree are equal, if possible Algorithm:1 Insert into the tree the middle element of the array2 Insert (into the left subtree) the left subarray elements3 阅读全文
posted @ 2013-04-11 20:26 曾先森在努力 阅读(234) 评论(0) 推荐(0) 编辑
摘要: 【试题描述】定义一个函数,输入判断一个树是否是另一个对的子树You have two very large binary trees: T1, with millions of nodes, and T2, with hun-dreds of nodes Create an algorithm to decide if T2 is a subtree of T1Note that the problem here specifies that T1 has millions of nodes—this means that we should be careful of how much sp 阅读全文
posted @ 2013-04-11 20:24 曾先森在努力 阅读(265) 评论(0) 推荐(0) 编辑
摘要: 【试题描述】定义一个函数,输入一个链表,删除无序链表中重复的节点【参考代码】方法一:Without a buffer, we can iterate with two pointers: “current” does a normal iteration, while“runner” iterates through all prior nodes to check for dups Runner will only see one dupper node, because if there were multiple duplicates they would have been remov 阅读全文
posted @ 2013-04-11 20:19 曾先森在努力 阅读(255) 评论(0) 推荐(0) 编辑
摘要: 【试题描述】定义一个函数,输入一个链表,判断链表是否存在环路平衡二叉树,又称AVL树。它或者是一棵空树,或者是具有下列性质的二叉树:它的左子树和右子树都是平衡二叉树,且左子树和右子树的高度之差之差的绝对值不超过1。问题:判断一个二叉排序树是否是平衡二叉树这里是二叉排序树的定义解决方案:根据平衡二叉树的定义,如果任意节点的左右子树的深度相差不超过1,那这棵树就是平衡二叉树。首先编写一个计算二叉树深度的函数,利用递归实现。【参考代码】方法一: 1 //返回树的最大深度 2 public static int Depth(Node root) 3 { 4 if (... 阅读全文
posted @ 2013-04-11 20:05 曾先森在努力 阅读(328) 评论(0) 推荐(0) 编辑
摘要: 【试题描述】定义一个函数,输入一个链表,判断链表是否存在环路,并找出回路起点Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked listEXAMPLEInput: A -> B -> C -> D -> E -> C [the same C as earlier]Output: CIf we move two pointers, one wi 阅读全文
posted @ 2013-04-11 19:53 曾先森在努力 阅读(403) 评论(0) 推荐(0) 编辑
摘要: 【试题描述】定义一个函数,字符串转数组数组转字符串【参考代码】 1 public static int strToInt(String str) 2 { 3 int i = 0, num = 0; 4 char[] strTemp = str.toCharArray(); 5 boolean isNeg = false; 6 int len = str.length(); 7 8 if (strTemp[0] == '-') 9 {10 isNeg = tr... 阅读全文
posted @ 2013-04-11 19:50 曾先森在努力 阅读(222) 评论(0) 推荐(0) 编辑
摘要: 【试题描述】定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点【参考代码】方法一: 1 public static Link reverseLinkList(Link head) 2 { 3 if (head == null || head.next == null) 4 return head; 5 6 Link pre = null; 7 Link cur = head; 8 Link back = head.next; 9 10 while (ba... 阅读全文
posted @ 2013-04-11 19:46 曾先森在努力 阅读(198) 评论(0) 推荐(0) 编辑
摘要: 【试题描述】求二叉树中任意两个节点的最近公共祖先也称为LCA问题(Lowest Common Ancestor)。二叉查找树如果该二叉树是二叉查找树,那么求解LCA十分简单。基本思想为:从树根开始,该节点的值为t,如果t大于t1和t2,说明t1和t2都位于t的左侧,所以它们的共同祖先必定在t的左子树中,从t.left开始搜索;如果t小于t1和t2,说明t1和t2都位于t的右侧,那么从t.right开始搜索;如果t1 right) { 7 int temp = left; 8 left = right; 9 right = temp;10 ... 阅读全文
posted @ 2013-04-11 19:33 曾先森在努力 阅读(433) 评论(0) 推荐(0) 编辑
摘要: 【试题描述】输入一个字符串,输出该字符串中字符的所有组合。举个例子,如果输入abc,它的组合有a、b、c、ab、ac、bc、abc。分析:这是一道很好的考查对递归理解的编程题,因此在过去一年中频繁出现在各大公司的面试、笔试题中。思路:同样是用递归求解。可以考虑求长度为n的字符串中m个字符的组合,设为C(n,m)。原问题的解即为C(n, 1), C(n, 2),...C(n, n)的总和。对于求C(n, m),从第一个字符开始扫描,每个字符有两种情况,要么被选中,要么不被选中,如果被选中,递归求解C(n-1, m-1)。如果未被选中,递归求解C(n-1, m)。不管哪种方式,n的值都会减少,递. 阅读全文
posted @ 2013-04-11 17:13 曾先森在努力 阅读(234) 评论(0) 推荐(0) 编辑