《微软面试一百道题目精选》由会员分享,可在线阅读,更多相关《微软面试一百道题目精选(70页珍藏版)》请在金锄头文库上搜索。
1、编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第1页 共1页第9 题判断整数序列是不是二元查找树的后序遍历结果题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:8/ 6 10/ / 5 7 9 11因此返回true。如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。ANSWER:This is an interesting one. There is a traditional question that
2、 requires the binary tree to be re-constructed from mid/post/pre order results. This seems similar. For the problems related to (binary) trees, recursion is the first choice.In this problem, we know in post-order results, the last number should be the root. So we have known the root of the BST is 8
3、in the example. So we can split the array by the root.int isPostorderResult(int a, int n) return helper(a, 0, n-1);int helper(int a, int s, int e) if (e=s) return 1; int i=e-1; while (aeai & i=s) i-; if (!helper(a, i+1, e-1) return 0; int k = l; while (ae=s) i-; return helper(a, s, l);第10 题翻转句子中单词的顺
4、序。题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。例如输入“I am a student.”,则输出“student. a am I”。Answer:Already done this. Skipped.第11 题求二叉树中节点的最大距离.如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义距离为两节点之间边的个数。写一个程序,求一棵二叉树中相距最远的两个节点之间的距离。ANSWER:This is interesting. Also recursively, the longest d
5、istance between two nodes must be either from root to one leaf, or between two leafs. For the former case, its the tree height. For the latter case, it should be the sum of the heights of left and right subtrees of the two leaves most least ancestor.The first case is also the sum the heights of subt
6、rees, just the height + 0.int maxDistance(Node * root) int depth; return helper(root, depth);int helper(Node * root, int &depth) if (root = NULL) depth = 0; return 0; int ld, rd; int maxleft = helper(root-left, ld); int maxright = helper(root-right, rd); depth = max(ld, rd)+1; return max(maxleft, ma
7、x(maxright, ld+rd);第12 题题目:求1+2+n,要求不能使用乘除法、for、while、if、else、switch、case 等关键字以及条件判断语句(A?B:C)。ANSWER:1+.+n=n*(n+1)/2=(n2+n)/2it is easy to get x/2, so the problem is to get n2though no if/else is allowed, we can easilly go around using short-pass.using macro to make it fancier:#define T(X, Y, i) (Y
8、& (1i) & X+=(Y 1;第13 题:题目:输入一个单向链表,输出该链表中倒数第k 个结点。链表的倒数第0 个结点为链表的尾指针。链表结点定义如下:struct ListNodeint m_nKey;ListNode* m_pNext;Answer:Two ways. 1: record the length of the linked list, then go n-k steps. 2: use two cursors.Time complexities are exactly the same.Node * lastK(Node * head, int k) if (k0) er
9、ror(“k 0;k-) if (pk-next!=NULL) pk = pk-next; else return NULL; while (pk-next!=NULL) p=p-next, pk=pk-next; return p;第14 题:题目:输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。例如输入数组1、2、4、7、11、15 和数字15。由于4+11=15,因此输出4 和11。ANSWER:Use two cursors. One at front and
10、the other at the end. Keep track of the sum by moving the cursors.void find2Number(int a, int n, int dest) int *f = a, *e=a+n-1; int sum = *f + *e; while (sum != dest & f e) if (sum left), &(root-right); mirror(root-left); mirror(root-right);void mirrorIteratively(Node * root) if (root = NULL) retur
11、n; stack buf; buf.push(root); while (!stack.empty() Node * n = stack.pop(); swap(&(root-left), &(root-right); if (root-left != NULL) buf.push(root-left); if (root-right != NULL) buf.push(root-right); 第16 题:题目(微软):输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。例如输入8/ 6 10/ / 5 7 9 11输出8 6 10 5 7 9 11。ANSWER:
12、The nodes in the levels are printed in the similar manner their parents were printed. So it should be an FIFO queue to hold the level. I really dont remember the function name of the stl queue, so I will write it in Java.void printByLevel(Node root) Node sentinel = new Node(); LinkedList q=new LinkedList(); q.addFirst(root); q.addFirst(sen