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" - someoneApproach
Create a joint node
Implementation
1var mergeTrees = function (root1, root2) {2 if (!root1) return root23 if (!root2) return root145 const root = new TreeNode(root1.val + root2.val)6 root.left = mergeTrees(root1.left, root2.left)7 root.right = mergeTrees(root1.right, root2.right)89 return root10}
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.