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

Leetcode Problem 606. Construct String from Binary Tree

606. Construct String from Binary Tree

Leetcode Solutions

Recursive Preorder Traversal

  1. Start with the root node of the binary tree.
  2. If the current node is null, return an empty string.
  3. Convert the value of the current node to a string and append it to the result.
  4. Recursively call the function for the left child. If the left child is not null, enclose the result of the recursion in parentheses and append it to the result string.
  5. Recursively call the function for the right child. If the right child is not null but the left child is, append an empty pair of parentheses before appending the result of the right child recursion enclosed in parentheses.
  6. If both children are null, do not append any parentheses.
  7. Return the result string.
UML Thumbnail

Iterative Method Using Stack

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...