Binary Tree Level Order Traversal (LeetCode 102) – Problem Description and Solutions in Java, C++, C, and Python
The article humorously discusses Huawei cafeteria competition before presenting LeetCode problem 102, which requires a breadth‑first level order traversal of a binary tree, and provides complete Java, C++, C, and Python implementations that use a queue to process each tree level.
The article begins with a humorous observation about competition between two Huawei cafeterias, likening it to market competition and noting the impact of performance rankings.
It then introduces LeetCode problem 102, which asks for a level‑order (breadth‑first) traversal of a binary tree, providing the problem description, input and output examples, and constraints on node values and tree size.
After the problem statement, detailed solution code is presented in four languages—Java, C++, C, and Python—each using a queue to perform BFS, count nodes per level, and collect values into a list of lists representing each tree level.
All code snippets are shown in full and wrapped in tags to preserve their original formatting.
Java solution:
public List
> levelOrder(TreeNode root) {
List
> ans = new LinkedList<>();
if (root == null)
return ans;
Queue
queue = new LinkedList<>(); // 创建队列
queue.offer(root); // 把根节点添加到队列中
while (!queue.isEmpty()) {
int levelCount = queue.size(); // 每层的节点个数
List
subList = new ArrayList<>();
for (int i = 0; i < levelCount; i++) {
TreeNode cur = queue.poll(); // 节点出队
subList.add(cur.val);
if (cur.left != null)
queue.offer(cur.left);
if (cur.right != null)
queue.offer(cur.right);
}
ans.add(subList); // 当前层的集合添加到队列中。
}
return ans;
}C++ solution:
vector
> levelOrder(TreeNode *root) {
vector
> ans;
if (root == nullptr)
return ans;
queue
q; // 创建队列
q.push(root); // 把根节点添加到队列中
while (!q.empty()) {
int levelCount = q.size(); // 每层的节点个数
vector
subList;
for (int i = 0; i < levelCount; i++) {
TreeNode *cur = q.front();
q.pop(); // 节点出队
subList.push_back(cur->val);
if (cur->left)
q.push(cur->left);
if (cur->right)
q.push(cur->right);
}
ans.push_back(subList); // 当前层的集合添加到队列中。
}
return ans;
}C solution:
int **levelOrder(struct TreeNode *root, int *returnSize, int **returnColumnSizes) {
*returnSize = 0;
int **ans = (int **)malloc(sizeof(int *) * 2001);
if (root == NULL)
return ans;
*returnColumnSizes = malloc(sizeof(int) * 2001);
struct TreeNode **q = (struct TreeNode **)malloc(sizeof(struct TreeNode *) * 2001); // 创建队列
int left = 0, right = 0; // 左闭右开
q[right++] = root; // 把根节点添加到队列中
while (left != right) {
int levelCount = right - left; // 每层的节点个数
int *subList = (int *)malloc(sizeof(int) * levelCount);
for (int i = 0; i < levelCount; i++) {
struct TreeNode *cur = q[left++]; // 节点出队
subList[i] = cur->val;
if (cur->left)
q[right++] = cur->left;
if (cur->right)
q[right++] = cur->right;
}
(*returnColumnSizes)[*returnSize] = levelCount;
ans[(*returnSize)++] = subList; // 当前层的集合添加到队列中。
}
return ans;
}Python solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
ans = []
if root is None:
return ans
q = deque([root]) # 创建队列
while q:
levelCount = len(q) # 每层的节点个数
subList = []
for _ in range(levelCount):
cur = q.popleft() # 节点出队
subList.append(cur.val)
if cur.left:
q.append(cur.left)
if cur.right:
q.append(cur.right)
ans.append(subList) # 当前层的集合添加到队列中。
return ansIT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.