LeetCode: Balanced Binary Tree Solution
Modified version of "Maximum Depth of Binary Tree"Approach
During the calculation of tree height, check the different in height between two subtrees
Implementation
1var isBalanced = function (root) {2 let res = true34 const recursion = node => {5 if (!node) return 067 const leftHeight = recursion(node.left)8 const rightHeight = recursion(node.right)910 res &&= Math.abs(leftHeight - rightHeight) <= 11112 return Math.max(leftHeight, rightHeight) + 113 }1415 recursion(root)1617 return res18}
References
Similar problems
Comments
Loading comments...
Tags
leetcode
recursion
tree
binary tree
dfs
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.