LeetCode: Merge Two Binary Trees Solution

"You know you don't get bonus points for squishing all your code into the least number of lines" - someone

Approach

Create a joint node

Implementation

1var mergeTrees = function (root1, root2) {
2 if (!root1) return root2
3 if (!root2) return root1
4
5 const root = new TreeNode(root1.val + root2.val)
6 root.left = mergeTrees(root1.left, root2.left)
7 root.right = mergeTrees(root1.right, root2.right)
8
9 return root
10}

Comments

Loading comments...

Tags

leetcode

tree

recursion

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

CSSBattle 15.82: Diamond Cut

Absolute position, pseudo-elements, transforms

Previous Post

HoningJS

Search Posts