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

Leetcode Problem 129. Sum Root to Leaf Numbers

129. Sum Root to Leaf Numbers

Leetcode Solutions

Recursive Preorder Traversal

  1. Define a helper function that takes a node and the current number formed.
  2. If the node is null, return 0.
  3. Update the current number by shifting it one place to the left and adding the node's value.
  4. If the node is a leaf, return the current number.
  5. Recursively call the helper function for the left and right children, adding their results together, and return this sum.
  6. Call the helper function with the root node and 0 as the initial current number.
UML Thumbnail

Iterative Preorder Traversal

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...