LeetCode: Symmetric Tree Solution
RecursionApproach
Base cases:
- falsewhen either left or right is null
- truewhen both left and right are null
- falsewhen both values of left and right are not equal
Implementation
1var isSymmetric = function (root) {2 if (!root) return true34 const recursion = (left, right) => {5 if ((!left && right) || (left && !right)) return false6 if (!left && !right) return true7 if (left.val !== right.val) return false8 return (9 recursion(left.left, right.right) && recursion(left.right, right.left)10 )11 }1213 return recursion(root.left, root.right)14}
References
Comments
Loading comments...
Tags
leetcode
tree
binary 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.