news 2026/6/3 2:14:53

0531核心代码编程-企业内部门的最大层级-100分

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
0531核心代码编程-企业内部门的最大层级-100分

题目描述:企业的组织架构以树形结构表示,每个节点包含:
left: 左子部门(第一个子部门)
right 右子部门(第二个子部门)
为了优化管理结构,实现扁平化管理,需要计算企业的最大管理层级深度。
请计算企业的部门层级的最大深度。
注意:
1、一个部门最多能有2个直属的子部门(二叉树);
2、输入由数字和特殊符号#组成的序列,总结点数不超过1024个; 数字表示该位置有子部门,#表示该位置无子部门(即无此节点)。

示例1
输入:["1","#","2","#","3","#","4","#","5"]
输出:5

示例2
输入:["1","2","3","4","5","6","7","8","9"]
输出:4

示例3
输入:["1","2","#"]

输出:2

根据题目要求,计算企业的部门层级最大深度,即二叉树的最大深度。输入为层序遍历序列(包含空节点"#"),需重建二叉树后计算深度。以下为各语言实现:

C++

#include <queue> #include <vector> #include <string> #include <algorithm> using namespace std; int max_depth(vector<string>& arr) { if (arr.empty() || arr[0] == "#") return 0; int n = arr.size(); int next_index = 1; queue<int> depths; depths.push(1); int max_depth = 1; while (!depths.empty() && next_index < n) { int depth = depths.front(); depths.pop(); for (int i = 0; i < 2; i++) { if (next_index >= n) break; if (arr[next_index] != "#") { int child_depth = depth + 1; max_depth = max(max_depth, child_depth); depths.push(child_depth); } next_index++; } } return max_depth; }

Java

import java.util.*; class Solution { public int max_depth(String[] arr) { if (arr.length == 0 || "#".equals(arr[0])) return 0; int n = arr.length; int nextIndex = 1; Queue<Integer> depths = new LinkedList<>(); depths.add(1); int maxDepth = 1; while (!depths.isEmpty() && nextIndex < n) { int depth = depths.poll(); for (int i = 0; i < 2; i++) { if (nextIndex >= n) break; if (!"#".equals(arr[nextIndex])) { int childDepth = depth + 1; maxDepth = Math.max(maxDepth, childDepth); depths.add(childDepth); } nextIndex++; } } return maxDepth; } }

JavaScript

function max_depth(arr) { if (arr.length === 0 || arr[0] === '#') return 0; const n = arr.length; let nextIndex = 1; const depths = [1]; let maxDepth = 1; while (depths.length > 0 && nextIndex < n) { const depth = depths.shift(); for (let i = 0; i < 2; i++) { if (nextIndex >= n) break; if (arr[nextIndex] !== '#') { const childDepth = depth + 1; maxDepth = Math.max(maxDepth, childDepth); depths.push(childDepth); } nextIndex++; } } return maxDepth; }

Go

package main func max_depth(arr []string) int { if len(arr) == 0 || arr[0] == "#" { return 0 } n := len(arr) nextIndex := 1 depths := []int{1} maxDepth := 1 for len(depths) > 0 && nextIndex < n { depth := depths[0] depths = depths[1:] for i := 0; i < 2; i++ { if nextIndex >= n { break } if arr[nextIndex] != "#" { childDepth := depth + 1 if childDepth > maxDepth { maxDepth = childDepth } depths = append(depths, childDepth) } nextIndex++ } } return maxDepth }

Python

from collections import deque def max_depth(arr): if not arr or arr[0] == '#': return 0 n = len(arr) next_index = 1 depths = deque([1]) max_depth_val = 1 while depths and next_index < n: depth = depths.popleft() for _ in range(2): if next_index >= n: break if arr[next_index] != '#': child_depth = depth + 1 max_depth_val = max(max_depth_val, child_depth) depths.append(child_depth) next_index += 1 return max_depth_val

C

#include <stdlib.h> typedef struct { int* data; int front, rear; int size; } Queue; Queue* createQueue(int n) { Queue* q = (Queue*)malloc(sizeof(Queue)); q->data = (int*)malloc(n * sizeof(int)); q->front = q->rear = -1; q->size = 0; return q; } void enqueue(Queue* q, int val) { q->data[++q->rear] = val; q->size++; } int dequeue(Queue* q) { q->size--; return q->data[++q->front]; } int max_depth(char** arr, int arrSize) { if (arrSize == 0 || strcmp(arr[0], "#") == 0) return 0; int next_index = 1; Queue* depths = createQueue(arrSize); enqueue(depths, 1); int max_depth_val = 1; while (depths->size > 0 && next_index < arrSize) { int depth = dequeue(depths); for (int i = 0; i < 2; i++) { if (next_index >= arrSize) break; if (strcmp(arr[next_index], "#") != 0) { int child_depth = depth + 1; if (child_depth > max_depth_val) max_depth_val = child_depth; enqueue(depths, child_depth); } next_index++; } } free(depths->data); free(depths); return max_depth_val; }

算法说明

  1. 输入处理:若输入数组为空或根节点为"#",直接返回深度0。
  2. 初始化:使用队列存储当前节点深度,根节点深度初始化为1。
  3. BFS遍历
    • 遍历输入数组,
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/3 2:12:01

答辩PPT制作效率翻倍!百考通AI学术PPT实战测评

对于高校学生、科研从业者来说&#xff0c;开题汇报、中期答辩、毕业结题、课题成果展示是学业科研路上的必经环节。一份逻辑清晰、排版规范、内容精炼的PPT&#xff0c;是顺利通过考核、展现研究成果的核心加分项。 但很多人都面临同一个困境&#xff1a;专业研究能力扎实、论…

作者头像 李华
网站建设 2026/6/3 2:09:59

3步搞定:抖音无水印下载工具高效解决方案

3步搞定&#xff1a;抖音无水印下载工具高效解决方案 【免费下载链接】douyin-downloader A practical Douyin downloader for both single-item and profile batch downloads, with progress display, retries, SQLite deduplication, and browser fallback support. 抖音批量…

作者头像 李华
网站建设 2026/6/3 1:59:50

企业决策者必读:如何评估必应推广是否适合你的业务?

企业决策者必读&#xff1a;如何评估必应推广是否适合你的业务&#xff1f;决策者的困惑作为企业决策者&#xff0c;你可能经常面临这样的问题&#xff1a;市场上推广渠道这么多&#xff0c;哪个真正适合我的业务&#xff1f;预算有限&#xff0c;应该先试哪个平台&#xff1f;…

作者头像 李华
网站建设 2026/6/3 1:58:02

基于Arduino与Unity的自制游戏控制器:从硬件搭建到软件交互全解析

1. 项目概述&#xff1a;从零打造你的专属游戏控制器作为一个喜欢鼓捣硬件和游戏开发的爱好者&#xff0c;我总觉得市面上那些千篇一律的游戏手柄少了点“灵魂”。它们功能强大&#xff0c;但无法承载我们独特的创意。于是&#xff0c;我萌生了一个想法&#xff1a;为什么不自己…

作者头像 李华
网站建设 2026/6/3 1:55:59

保姆级教程:用davfs2在Ubuntu 22.04上挂载WebDAV(含secrets文件权限避坑)

深度实践&#xff1a;Ubuntu 22.04下WebDAV安全挂载全流程解析WebDAV作为企业文件共享的轻量级解决方案&#xff0c;在混合办公环境中展现出独特优势。不同于SMB或NFS协议&#xff0c;它基于HTTP/HTTPS传输的特性使其能穿透大多数企业防火墙&#xff0c;同时保持与原生文件系统…

作者头像 李华