Press "Enter" to skip to content

Posts tagged as “BFS”

花花酱 LeetCode 637. Average of Levels in Binary Tree



Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

Example 1:


Time Complexity:


Space Complexity:


Solution 1:


Solution 2:



Related Problems:

花花酱 LeetCode 102. Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],

return its level order traversal as:

Solution 1: BFS O(n)

Solution 2: DFS O(n)