Google模拟面试【面试】

Google模拟面试【面试】

2023-12-25 16:00:42

Google代码面试

Prompt #1

给一个二叉树,定义深度为结点到根;所要遍历的边的数量。
示例二叉树中8的深度为3,1的深度为0。
编写函数返回这个二叉树的所有结点的深度和。
示例二叉树答案是16

         1/  \2      3/ \    / \4   5  6   7/ \8   9
public class Main {static class Node{int val;Node left;Node right;public Node(int v){val=v;}}public static void main(String[] args) {Node a=new Node(1);Node b=new Node(2);Node c=new Node(3);Node d=new Node(4);Node e=new Node(5);Node f=new Node(6);Node g=new Node(7);Node h=new Node(8);Node i=new Node(9);a.left=b;a.right=c;b.left=d;b.right=e;c.left=f;c.right=g;d.left=h;d.right=i;int res1=solve1(a);System.out.println(res1);//16}public static int solve1(Node root){sum=0;sumDepths(root,0);return  sum;}static int sum;public static void sumDepths(Node root,int depth){sum+=depth;if(root.left!=null){sumDepths(root.left,depth+1);}if(root.right!=null){sumDepths(root.right,depth+1);}}}

Prompt #2

返回每一个结点的子树深度的和,求和

示例二叉树答案是26

树型dp
对于结点x,有两个孩子,y,z
x子树结点个数=1+y的子树结点个数+z的子树结点个数
x子树深度和=(y的子树结点个数+y的子树深度和)+(z的子树结点个数+z的子树深度和)
因为对于孩子y的每个结点来说,其对于y的双亲x的深度都是加了x->y的一条边。
特殊:叶子结点(1,0)

过程分析
8,9,5,6,7:(1,0)
4,3:(3,2)
2:(5,6)
1:(9,16)

2+2+6+16=26

	public static int solve2(Node root) {ans=0;dfs(root);return ans;}static class Info{int numNodes;int sumDepths;public Info(int a,int b){numNodes=a;sumDepths=b;}}static int ans;static Info dfs(Node root){Info pInfo=new Info(1,0);if(root.left!=null){Info cInfo=dfs(root.left);pInfo.sumDepths+=cInfo.sumDepths+cInfo.numNodes;pInfo.numNodes+=cInfo.numNodes;}if(root.right!=null){Info cInfo=dfs(root.right);pInfo.sumDepths+=cInfo.sumDepths+cInfo.numNodes;pInfo.numNodes+=cInfo.numNodes;}ans+= pInfo.sumDepths;return  pInfo;}

Prompt #3

求出所有节点到目标结点的深度和
比如到4的深度和是18

         1/  \2      3/ \    / \4   5  6   7/ \8   9

分析可以得出
到一个目标结点的深度和=
到根结点的目标和-子树中的结点数量+其他结点数量(子树之外的结点数)
其他结点数量=总结点数量-子树结点数量

dfs1把每个子树及其对应的结点数存入到map中
运行结果

<Node1,9>,<Node2,5>,<Node3,3>,<Node4,3>
<Node5,1>,<Node6,1>,<Node7,1>,<Node8,1>,<Node9,1>,
n=root.numNodes=9
sumDists=root.sumDepth=16

模拟运行,忽略其他结点
dfs2(1,16,4)
左分支:newDists=16-5+(9-5)=15
dfs2(2,15,4)
右分支:newDists=15-3+(9-3)=18
dfs(4,18,4)
ans=18
会继续遍历所有结点,但是答案只会保存一个

   //视频中把每个子树的结点数信息放到结点中//我把其放到哈希表中static HashMap<Node,Integer> map;//总结点个数static int n;static int solve3(Node root,Node target){ans=0;map=new HashMap<>();sumDists(root,target);return ans;}//把每个子树的结点数放到对应结点和结点数的map中static Info dfs1(Node root){Info pInfo=new Info(1,0);if(root.left!=null){Info cInfo=dfs1(root.left);pInfo.sumDepths+=cInfo.sumDepths+cInfo.numNodes;pInfo.numNodes+=cInfo.numNodes;}if(root.right!=null){Info cInfo=dfs1(root.right);pInfo.sumDepths+=cInfo.sumDepths+cInfo.numNodes;pInfo.numNodes+=cInfo.numNodes;}//和dfs的区别只有此处map.put(root, pInfo.numNodes);return  pInfo;}static void dfs2(Node u,int sumDists,Node target){if(u==target){ans=sumDists;}if(u.left!=null){int newSumDists=sumDists-map.get(u.left)+(n-map.get(u.left));dfs2(u.left,newSumDists,target);}if(u.right!=null){int newSumDists=sumDists-map.get(u.right)+(n-map.get(u.right));dfs2(u.right,newSumDists,target);}}static int sumDists(Node root,Node target){Info info=dfs1(root);n=info.numNodes;dfs2(root,info.sumDepths,target);return ans;}

补充 #3用图来解决

到4的深度和为18

         1/  \2      3/ \    / \4   5  6   7/ \8   9
2,8,94的深度都是1
1,54的深度都是2
34的深度是3
6,74的深度都是4
1*3+2*2+3+2*4=18

一个bfs

    //用图的方法解决//设置访问数组static HashMap<Node,Boolean> visited;static int solve(Node root,Node target){ans=0;visited=new HashMap<>();//图,hashmap可以更快找到,而没有采取下标对应结点HashMap<Node,ArrayList<Node>> graph=new HashMap<>();//树转为双向图,并且设置访问数组toGraph(root,graph);//广度优先遍历bfs(graph,target);return ans;}//深度优先转为双向图static void toGraph(Node root,HashMap<Node,ArrayList<Node>> graph){if(!graph.containsKey(root)){graph.put(root,new ArrayList<>());}if(root.left!=null){graph.get(root).add(root.left);toGraph(root.left,graph);graph.get(root.left).add(root);}if(root.right!=null){graph.get(root).add(root.right);toGraph(root.right,graph);graph.get(root.right).add(root);}visited.put(root,false);}static void bfs(HashMap<Node,ArrayList<Node>> graph,Node target){visited.put(target,true);Queue<Node> queue=new ArrayDeque<>();//加入第一层for (Node v:graph.get(target)){queue.offer(v);}//第一层深度为1int height=1;while (!queue.isEmpty()){int size=queue.size();//每次遍历一层for (int i = 0; i < size; i++) {Node v=queue.poll();if (!visited.get(v)){//设置访问数组visited.put(v,true);//记录答案ans+=height;//下一层添加for (Node next:graph.get(v)) {queue.offer(next);}}}//下一次深度+1height++;}}

所有代码

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Queue;public class Main {static class Node{int val;Node left;Node right;public Node(int v){val=v;}}public static void main(String[] args) {Node a=new Node(1);Node b=new Node(2);Node c=new Node(3);Node d=new Node(4);Node e=new Node(5);Node f=new Node(6);Node g=new Node(7);Node h=new Node(8);Node i=new Node(9);a.left=b;a.right=c;b.left=d;b.right=e;c.left=f;c.right=g;d.left=h;d.right=i;int res1=solve1(a);System.out.println(res1);//16int res2=solve2(a);System.out.println(res2);//26int res3=solve3(a,d);System.out.println(res3);//18//使用图来解决#3int res=solve(a,d);System.out.println(res);//18}//--------------------------------------------------------------------public static int solve1(Node root){sum=0;sumDepths(root,0);return  sum;}static int sum;public static void sumDepths(Node root,int depth){sum+=depth;if(root.left!=null){sumDepths(root.left,depth+1);}if(root.right!=null){sumDepths(root.right,depth+1);}}//--------------------------------------------------------------------public static int solve2(Node root) {ans=0;dfs(root);return ans;}static class Info{int numNodes;int sumDepths;public Info(int a,int b){numNodes=a;sumDepths=b;}}static int ans;static Info dfs(Node root){Info pInfo=new Info(1,0);if(root.left!=null){Info cInfo=dfs(root.left);pInfo.sumDepths+=cInfo.sumDepths+cInfo.numNodes;pInfo.numNodes+=cInfo.numNodes;}if(root.right!=null){Info cInfo=dfs(root.right);pInfo.sumDepths+=cInfo.sumDepths+cInfo.numNodes;pInfo.numNodes+=cInfo.numNodes;}ans+= pInfo.sumDepths;return  pInfo;}//--------------------------------------------------------------------//视频中把每个子树的结点数信息放到结点中//我把其放到哈希表中static HashMap<Node,Integer> map;//总结点个数static int n;static int solve3(Node root,Node target){ans=0;map=new HashMap<>();sumDists(root,target);return ans;}//把每个子树的结点数放到对应结点和结点数的map中static Info dfs1(Node root){Info pInfo=new Info(1,0);if(root.left!=null){Info cInfo=dfs1(root.left);pInfo.sumDepths+=cInfo.sumDepths+cInfo.numNodes;pInfo.numNodes+=cInfo.numNodes;}if(root.right!=null){Info cInfo=dfs1(root.right);pInfo.sumDepths+=cInfo.sumDepths+cInfo.numNodes;pInfo.numNodes+=cInfo.numNodes;}//和dfs的区别只有此处map.put(root, pInfo.numNodes);return  pInfo;}static void dfs2(Node u,int sumDists,Node target){if(u==target){ans=sumDists;}if(u.left!=null){int newSumDists=sumDists-map.get(u.left)+(n-map.get(u.left));dfs2(u.left,newSumDists,target);}if(u.right!=null){int newSumDists=sumDists-map.get(u.right)+(n-map.get(u.right));dfs2(u.right,newSumDists,target);}}static int sumDists(Node root,Node target){Info info=dfs1(root);n=info.numNodes;dfs2(root,info.sumDepths,target);return ans;}//--------------------------------------------------------------------//用图的方法解决//设置访问数组static HashMap<Node,Boolean> visited;static int solve(Node root,Node target){ans=0;visited=new HashMap<>();//图,hashmap可以更快找到,而没有采取下标对应结点HashMap<Node,ArrayList<Node>> graph=new HashMap<>();//树转为双向图,并且设置访问数组toGraph(root,graph);//广度优先遍历bfs(graph,target);return ans;}//深度优先转为双向图static void toGraph(Node root,HashMap<Node,ArrayList<Node>> graph){if(!graph.containsKey(root)){graph.put(root,new ArrayList<>());}if(root.left!=null){graph.get(root).add(root.left);toGraph(root.left,graph);graph.get(root.left).add(root);}if(root.right!=null){graph.get(root).add(root.right);toGraph(root.right,graph);graph.get(root.right).add(root);}visited.put(root,false);}static void bfs(HashMap<Node,ArrayList<Node>> graph,Node target){visited.put(target,true);Queue<Node> queue=new ArrayDeque<>();//加入第一层for (Node v:graph.get(target)){queue.offer(v);}//第一层深度为1int height=1;while (!queue.isEmpty()){int size=queue.size();//每次遍历一层for (int i = 0; i < size; i++) {Node v=queue.poll();if (!visited.get(v)){//设置访问数组visited.put(v,true);//记录答案ans+=height;//下一层添加for (Node next:graph.get(v)) {queue.offer(next);}}}//下一次深度+1height++;}}}

2023-12-25 19:10:43

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/578415.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Openstack开启虚拟化嵌套

好久没写东西了&#xff0c;前两天我准备在虚机上装一个vmware 的虚机&#xff0c;结果失败了&#xff0c;提示如下&#xff0c;由于我是虚机上安装虚机&#xff0c;我的宿主机肯定是开启了vt-x和vt-d的 查了一些资料&#xff0c;这个需要打开nested,先看看nested返回是否为Y&a…

Unity向量按照某一点进行旋转

Unity向量按照某一点进行旋转 一、unity的旋转二、向量按照原点进行旋转注意案例 三、向量按照指定位置进行旋转案例 一、unity的旋转 首先要知道一点就是在Unity的旋转中使用过四元数进行旋转的&#xff0c;如果对一个物体的rotation直接赋值你会发现结果不是你最终想要的结果…

1111111111111111111

11111111111111111111111111

Astro学习使用记录

Astro学习使用记录 前言Astro是什么&#xff1f;问题记录1. 使用组件库2. pages 目录下不要放 除 .astro 文件以外的文件 总结 前言 Astro的出现 为了追求前端应用的性能与速度&#xff0c;近年前端界涌现出许多的解决方案&#xff0c;像SSR、SSG解决方案再到今天的island架构…

迎新辞旧,欢度元旦

迎新辞旧&#xff0c;欢度元旦 新年钟声即将敲响&#xff0c;欢度元旦的时刻即将来临。在这个美好的时刻&#xff0c;我们纷纷辞旧迎新&#xff0c;放飞自我追逐梦想的翅膀。让羊大师带大家一起来庆祝新年的到来&#xff0c;共同创造美好的开始&#xff01; 一、迎新辞旧&…

希尔排序详解(C语言)

前言 希尔排序是一种基于插入排序的快速排序算法。所以如果还会插入排序的小伙伴可以点击链接学习一下插入排序&#xff08;点我点我&#xff01;&#xff09; &#xff0c;相较于插入排序&#xff0c;希尔排序拥有更高的效率&#xff0c;小伙伴们肯定已经迫不及待学习了吧&…

OPNET Modeler帮助文档的打开方式

前面有篇文章修改OPNET帮助文档的默认打开浏览器 & 给Edge浏览器配置IE Tab插件已经提到了打开OPNET Modeler打开帮助文档的方法&#xff0c;有时候打开时会显示如下。 界面中没有什么内容加载出来&#xff01;我是在Google浏览器中打开的&#xff0c;其他的浏览器也是一样…

10000个jpg图片文件如何按数量放在20个文件夹中的方法

日常工作中在处理大量文件时&#xff0c;如何有效地将它们分类和管理变得至关重要。在这种情况下&#xff0c;有10000个jpg图片&#xff0c;要如何把它们按数量放在20个文件夹&#xff0c;如何避免手动操作呢&#xff1f;现在一起来看看云炫文件管理器批量移动文件的具体操作吧…

智慧燃气为 “ 城市生命线 ” 打造“看得见”的安全

关键词&#xff1a;智慧燃气、智慧燃气平台、智慧燃气管网、燃气数字化、智慧燃气系统、智慧燃气解决方案 近年来&#xff0c;随着互联网技术、物联网技术、大数据、云计算技术的飞速发展&#xff0c;“互联网”已成为重要的国家战略&#xff0c;各行业都在寻求智能化转型之路…

flowable任务分配方式篇动态部门经理:固定分配、表达式分配、监听器分配

这里写自定义目录标题 1、固定分配2、表达式分配在此流程部署了之后&#xff0c;在流程定义信息表中可以看到此流程的相关信息启动流程查看我们设置的变量在task表中&#xff0c;可以看到当前分配人是张三&#xff0c;说明值表达式被解析了 完成任务在张三完成任务之后&#xf…

遇到DDOS了怎么去防御

DDOS攻击原理是什么 DDoS攻击的工作原理是通过控制发送大量的恶意流量&#xff0c;让目标网站瘫痪或服务器宕机&#xff0c;从而无法正常响应合法流量的访问请求。当你要访问某一主机或网站时&#xff0c;首先&#xff0c;将数据包发送到目标主机&#xff0c;并发出连接请求。…

为什么pmp证书只能对标cspm二级证书?

拿PMP认证可以直接认证CSPM&#xff1f;快来看看你符合哪个等级&#xff01; PMP认证大家了解的可能比较多&#xff0c;它推出的时间较长&#xff0c;并且仅在国内拿到认证的人数也与日俱增。而CSPM相对来说大家就比较陌生了&#xff0c;它是哪些部门发起的&#xff0c;有什么用…

云渲染UE4像素流送搭建(winows、ubuntu单实例与多实例像素流送)

windows/ubuntu20.4下UE4.27.2像素流送 像素流送技术可以将服务器端打包的虚幻引擎应用程序在客户端的浏览器上运行&#xff0c;用户可以通过浏览器操作虚幻引擎应用程序&#xff0c;客户端无需下载虚幻引擎&#xff0c;本文实现两台机器通过物理介质网线实现虚幻引擎应用程序…

【小白专用】C# 压缩文件 ICSharpCode.SharpZipLib.dll效果:

插件描述&#xff1a; ICSharpCode.SharpZipLib.dll 是一个完全由c#编写的Zip, GZip、Tar 、 BZip2 类库,可以方便地支持这几种格式的压缩解压缩, SharpZipLib 的许可是经过修改的GPL&#xff0c;底线是允许用在不开源商业软件中&#xff0c;意思就是免费使用。具体可访问ICSha…

图片素材管理软件Eagle for mac提高素材整理维度

Eagle for mac是一款图片素材管理软件&#xff0c;支持藏网页图片&#xff0c;网页截屏&#xff0c;屏幕截图和标注&#xff0c;自动标签和筛选等功能&#xff0c;让你设计师方便存储需要的素材和查找&#xff0c;提供工作效率。 Eagle mac软件介绍 Eagle mac帮助你成为更好、…

比宜德停业,奥乐齐死磕,硬折扣该怎样长硬不衰?

作者 | 楚文龙 来源 | 洞见新研社 刚刚过去的周末&#xff0c;让零售行业的从业者神经紧绷。因为&#xff0c;12月23日多个信源曝出&#xff0c;社区硬折扣超市比宜德已公告于12月22日起暂停营业。 作为中国第一家&#xff0c;也是唯一一家规模最大的硬折扣社区连锁店零售商&…

计算机视觉五大技术

目前&#xff0c;计算机视觉是深度学习领域最热门的研究领域之一。计算机视觉实际上是一个跨领域的交叉学科&#xff0c;包括计算机科学&#xff08;图形、算法、理论、系统、体系结构&#xff09;&#xff0c;数学&#xff08;信息检索、机器学习&#xff09;&#xff0c;工程…

ERP系统的优缺点有哪些?

企业在考虑引进ERP系统时总是比较谨慎的&#xff0c;毕竟&#xff0c;ERP关乎着企业的整体运营和未来发展。因此&#xff0c;选择适合的ERP系统对企业未来的成功至关重要。要全面评估&#xff0c;看看它到底能给企业带来啥好处&#xff0c;又可能会有啥风险。 看完这篇回答&am…

如何进行块存储管理

目录 块存储概念 块存储&#xff08;云盘&#xff09;扩容 方式一&#xff1a;直接扩容现有云盘 方式二&#xff1a;创建一块新数据盘 方式三&#xff1a;在更换操作系统时&#xff0c;同时更换系统盘 块存储&#xff08;云盘&#xff09;变配 云盘变配操作步骤 块存储概…

【Web】Ctfshow Thinkphp3.2.3代码审计(3)

web574 这题与web573的区别在于进find()前先进了where()处理 跟进where() 我们假设传个1&#xff0c;和id拼接 发现会进到is_string的判断里&#xff0c;让$options[where]array("_string">"1") 之后传入到find()&#xff0c;和web573一样也是以数组…