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

Leetcode Problem 617. Merge Two Binary Trees

617. Merge Two Binary Trees

Leetcode Solutions

Approach # Using Recursion

  1. Define a recursive function mergeTrees that takes two nodes, one from each tree.
  2. If both nodes are null, return null.
  3. If one of the nodes is null, return the other node.
  4. If both nodes are not null, sum their values and update the value of the first node.
  5. Recursively call mergeTrees on the left children of both nodes, and set the left child of the first node to the result.
  6. Recursively call mergeTrees on the right children of both nodes, and set the right child of the first node to the result.
  7. Return the first node, which now represents the merged tree at this point in the traversal.
UML Thumbnail

Approach # Iterative Method

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...