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

Leetcode Problem 623. Add One Row to Tree

623. Add One Row to Tree

Leetcode Solutions

Approach # Using Recursion (DFS)

  1. Check if the target depth is 1. If so, create a new root node with the given val and set the original tree as its left child.
  2. If the target depth is greater than 1, call the recursive function insert starting from the root node with the current depth as 1.
  3. In the insert function, check if the current depth is one less than the target depth.
  4. If the current depth matches, create new nodes with the given val and attach the original left and right subtrees to these new nodes.
  5. If the current depth does not match, recursively call insert for the left and right subtrees, incrementing the depth.
  6. Continue the recursion until all nodes at the target depth have been processed.
UML Thumbnail

Approach # Using queue (BFS)

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...