Balanced binary tree
Easytreedfsrecursion
A tree is balanced if at every node the depths of the left and right subtrees differ by at most 1. Return true if it is.
Examples write a tree level by level, with null for a missing child: [3,9,20,null,null,15,7].
Example 1
Input: root = [3,9,20,null,null,15,7] Output: true
Example 2
Input: root = [1,2,2,3,3,null,null,4,4] Output: false
Constraints
- 0 ≤ number of nodes ≤ 5000
- -10⁴ ≤ Node.val ≤ 10⁴
Submit checks your solution against 10 tests, including hidden ones.
// Provided by the judge: keep these definitions
class TreeNode {
constructor(val = 0, left = null, right = null) {
this.val = val
this.left = left
this.right = right
}
}
/**
* @param {TreeNode|null} root
* @return {bool}
*/
function isBalanced(root) {
}