//定义一个树类 class TreeNode { constructor(val) { this.val = val; this.children = []; } } function treeHeight(root) { if (!root) { return 0; } let max = 0; for (let i = 0; i < root.children.length; i++) { const height = treeHeight(root.children[i]); if (height > max) { max = height; } } return max +1; } const root = new TreeNode(1); const node2 = new TreeNode(2); const node3 = new TreeNode(3); const node4 = new TreeNode(4); const node5 = new TreeNode(5); const node6 = new TreeNode(6); const node7 = new TreeNode(7); root.children.push(node2); root.children.push(node3); node2.children.push(node4); node2.children.push(node5); node3.children.push(node6); node5.children.push(node7); console.log("root->", treeHeight(root));

相关推荐

12-27 22:35
门头沟学院 Java
点赞 评论 收藏
分享
程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务