Leetcode Problem 1469. Find All The Lonely Nodes

1469. Find All The Lonely Nodes

Leetcode Solutions

Recursive Depth-First Search

  1. Define a helper function dfs that takes a node and a boolean indicating if it's lonely.
  2. If the current node is null, return immediately.
  3. If the current node is lonely (the boolean is true), add its value to the result list.
  4. Call dfs on the left child with true if the right child is null, otherwise false.
  5. Call dfs on the right child with true if the left child is null, otherwise false.
  6. Start the process by calling dfs on the root with false since the root is never lonely.
UML Thumbnail

Iterative Breadth-First Search

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...