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

Leetcode Problem 366. Find Leaves of Binary Tree

366. Find Leaves of Binary Tree

Leetcode Solutions

DFS (Depth-First Search) without sorting

  1. Define a recursive function getHeight(node) that returns the height of the given node.
  2. If the node is null, return -1.
  3. Calculate the height of the left and right children recursively.
  4. The height of the current node is 1 plus the maximum height of its children.
  5. Add the current node's value to the list corresponding to its height in the output list of lists.
  6. If the list for the current height does not exist, create it.
  7. Return the height of the current node.
  8. Call getHeight(root) to initiate the process.
  9. Return the output list of lists.
UML Thumbnail

DFS (Depth-First Search) with sorting

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...