Balanced Binary Tree LeetCode solution in JavaScript
1/**2* Definition for a binary tree node.3* function TreeNode(val, left, right) {4* this.val = (val===undefined ? 0 : val)5* this.left = (left===undefined ? null : left)6* this.right = (right===undefined ? null : right)7* }8*/9/**10* @param {TreeNode} root11* @return {boolean}12*/13var isBalanced = function (root) {14let result = true;15function Dfs(root) {16if (!root) return 0;17let left = Dfs(root.left);18let right = Dfs(root.right);19if (Math.abs(left - right) > 1) {20result = false;21return;22}23return 1 + Math.max(left, right);24}25Dfs(root);26return result;27};