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

Leetcode Problem 257. Binary Tree Paths

257. Binary Tree Paths

Leetcode Solutions

Recursive Depth-First Search

  1. Define a helper function that accepts a node and the current path string.
  2. If the current node is null, return without doing anything.
  3. Append the current node's value to the path string.
  4. If the current node is a leaf (no children), add the path string to the list of paths.
  5. If the current node has a left child, recursively call the helper function with the left child and the updated path string.
  6. If the current node has a right child, recursively call the helper function with the right child and the updated path string.
  7. Invoke the helper function with the root node and an empty path string to start the DFS.
UML Thumbnail

Iterative Depth-First Search with Stack

Ask Question

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

Suggested Answer

Answer
Code Diffs Compare
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR