LeetCode: Same Tree Solution
RecursionApproach
Check if both node of p
and q
are the same, then recursively check the child nodes
Implementation
1var isSameTree = function (p, q) {2 if (!p && !q) return true3 if (!p || !q) return false4 if (p.val !== q.val) return false5 return isSameTree(p.left, q.left) && isSameTree(p.right, q.right)6}
References
Comments
Loading comments...
Tags
leetcode
recursion
tree
binary tree
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
CodeWars: Recover a secret string from random triplets
It's easy to make thing more complicated
Previous Post
LeetCode: Sort Colors
Count and overwrite