bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 107. Binary Tree Level Order Traversal II

107. Binary Tree Level Order Traversal II

Leetcode Solutions

Approach: Recursion: DFS Preorder Traversal

  1. Define a helper function that takes a node and its level as arguments.
  2. If the node is not null, check if the current level is already present in the levels list.
  3. If not, add a new list to levels.
  4. Append the node's value to the list corresponding to its level.
  5. Recursively call the helper function for the left child, incrementing the level by 1.
  6. Recursively call the helper function for the right child, incrementing the level by 1.
  7. After the recursion completes, reverse the levels list to get the bottom-up order.
  8. Return the reversed levels list.
UML Thumbnail

Approach: Iteration: BFS Traversal

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...