64: Merge two binary trees

Given two binary trees, return a merged tree

Merge rules:

  • return sum if both nodes are exist
  • if a right node is null return left node
  • if a left node is null return right node

 

Example 1

Input:   one = 1    two = 2
              / \          \
             5   3          4

Output:        3
              / \
             5   7

Example 1

Input:   one = 1    two = 2
              /            \
             5              4

Output:        3
              / \
             5   4
Difficulty:Easy
Topic:Tree
Problem #:64
Helpful article