字节跳动前端面试题两道
1,多维数组扁平化。原数组[[0],[2,3,4],1,[1,[2,3]]],输出[0,2,3,4,1,1,2,3]
{ //判断当前数组是否有子数组 function hasChildArray(arr) { return arr.some(element => { if (Array.isArray(element)) { has = true; return true; } }); } let sourceArr = [[0], [2, 3, 4], 1, [1, [2, 3]]]; let result = [];
//递归 (function doFunc(arr) { if (hasChildArray(arr)) { for (let i = 0, l = arr.length; i < l; i++) { if (typeof arr[i] == "number") { result.push(arr[i]); } else if (Array.isArray(arr[i])) { doFunc(arr[i]); } } } else { result=result.concat(arr); } })(sourceArr); console.log(result); }
2,二叉树tree ,根节点是root,判断是否存在一条完整路径,其路径上节点的值之和为target,输出布尔值。
举例:下面的树,是否存在target=7的一条完整路径(从根节点到叶节点),该路径上各个节点之和=target?
1 2 3 4 5 | 6 / \ 2 3 /\ / -1 3 0 |
这版本1:该版本创建二叉树的方法比较“笨”
{ //定义节点的数据结构 class Node { constructor(value, left, right) { this .value = value; this .left = left; this .right = right; } } //定义二叉树 class BTree { constructor() { this .list = []; } addRoot(node) { if (node != null ) { this .list.push(node); } } addLeft(pNode, node) { this .list.push(node); pNode.left = node; } addRight(pNode, node) { this .list.push(node); pNode.right = node; } //根据数组中的索引返回node getNode(index) { return this .list[index]; } getNodeList() { return this .list; } } const lable = "MK" ; //创建示例中的二叉树 let bTree = new BTree(); //第一层 根节点 bTree.addRoot( new Node(6, null , null )); //第二层 bTree.addLeft(bTree.getNode(0), new Node(2, null , null )); bTree.addRight(bTree.getNode(0), new Node(3, null , null )); //第三层 bTree.addLeft(bTree.getNode(1), new Node(-1, null , null )); bTree.addRight(bTree.getNode(1), new Node(3, null , null )); bTree.addLeft(bTree.getNode(2), new Node(0, null , null )); function hasPathSum(node, target) { console.log(node.value); //根节点 if (!node.left && !node.right) { return node.value == target; } //左右子节点 return ( (node.left && hasPathSum(node.left, target - node.value)) || (node.right && hasPathSum(node.right, target - node.value)) ); } console.time(lable); console.log(hasPathSum(bTree.getNode(0), 11)); console.timeEnd(lable); } |
版本2:精简版
{ //定义二叉树 class BTree { constructor(middle, left, right) { if (middle!=undefined) { this .value=middle; if (left!=undefined) this .left = left; if (right!=undefined) this .right = right; } } } /** * 创建一个树 * arr:一个代表二叉树的多维数组 */ function makeBTree(arr) { if (arr) { if (arr.length == 1) return new BTree(arr[0], null , null ); return new BTree(arr[0], makeBTree(arr[1]), makeBTree(arr[2])); } } const lable = "MK" ; //创建示例中的二叉树 let bTree = makeBTree([6, [2, [-1], [3]], [3, [0]]]); // let bTree = makeBTree([6, [2],[-1]]); function hasPathSum(node, target) { console.log(node); //根节点 if (node.left==undefined && node.right==undefined) { return node.value == target; } //左子节点 return ( (node.left!=undefined && hasPathSum(node.left, target - node.value)) || (node.right!=undefined && hasPathSum(node.right, target - node.value)) ); } console.time(lable); console.log(hasPathSum(bTree, 11)); console.timeEnd(lable); } |
本博客所有随笔,若未明确标示为转载或未带有原文链接,皆为原创。
本博客所有随笔版权归博客园和kai.ma所有,欢迎转载,转载请保留:
本博客所有随笔版权归博客园和kai.ma所有,欢迎转载,转载请保留:
- 出处:http://kaima.cnblogs.com
- 作者:kai.ma
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?