下载此文档

2022年微软面试一百道题目精选.docx


文档分类:管理/人力资源 | 页数:约92页 举报非法文档有奖
1/92
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/92 下载此文档
文档列表 文档介绍
第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 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 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 (a[e]>a[i] && i>=s) i--;
  if (!helper(a,i+1,e-1))
    return 0;
  int k = l;
  while (a[e]<a[i] && i>=s) i--;
  return helper(a,s,l);
}
第10 题
翻转句子中单词顺序。
题目:输入一种英文句子,翻转句子中单词顺序,但单词内字符顺序不变。
句子中单词以空格符隔开。为简朴起见,标点符号和普通字母同样解决。
例如输入“I am a student.”,则输出“student. a am I”。
Answer:
Already done this. Skipped.
第11 题
求二叉树中节点最大距离...
如果咱们把二叉树当作一种图,父子节点之间连线当作是双向,
咱们姑且定义"距离"为两节点之间边个数。
写一种程序,
求一棵二叉树中相距最远两个节点之间距离。
ANSWER:
This is interesting... Also recursively,the longest distance between two nodes must be either from root to one leaf,or between two leafs. For the former case,it’s 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 subtrees,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,max(maxright,ld+rd));
}
第12 题
题目:求1+2+…+n,
规定不能使用乘除法、for、while、if、else、swit

2022年微软面试一百道题目精选 来自淘豆网m.daumloan.com转载请标明出处.

非法内容举报中心
文档信息
  • 页数92
  • 收藏数0 收藏
  • 顶次数0
  • 上传人读书百遍
  • 文件大小73 KB
  • 时间2021-11-28
最近更新